<?php
header('Content-Type: application/json; charset=utf-8');

require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/includes/db.php';
require_once dirname(__DIR__) . '/includes/functions.php';
require_once dirname(__DIR__) . '/includes/functions_notes.php';

try {
    $songId = (int)($_GET['song_id'] ?? 0);
    if ($songId <= 0) {
        jsonResponse(['success' => false, 'message' => '유효하지 않은 곡 ID입니다.'], 400);
        exit;
    }

    jsonResponse([
        'success' => true,
        'note' => ensureUtf8String(getSongNote($songId)['note_text'] ?? ''),
        'rating' => getSongRating($songId),
    ]);
} catch (Throwable $e) {
    if (function_exists('writeAppLog')) {
        writeAppLog('error', 'note_error', 'Track note load failed', ['song_id' => $_GET['song_id'] ?? null, 'error' => $e->getMessage()]);
    }
    jsonResponse(['success' => false, 'message' => '감상 정보를 불러오지 못했습니다.'], 500);
}
