<?php
require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/functions_notes.php';
require_once __DIR__ . '/includes/layout.php';

$filter = (string)($_GET['filter'] ?? 'top_rated');
$filters = [
    'top_rated' => '별점 높은 곡',
    'recent_notes' => '최근 메모한 곡',
    'has_notes' => '메모 있는 곡',
    'unrated' => '미평가 곡',
    'liked_unrated' => '좋아요했지만 미평가',
    'popular_no_notes' => '많이 들었지만 메모 없음',
    'has_lyrics' => '가사 있는 곡',
    'no_lyrics' => '가사 없는 곡',
];
if (!isset($filters[$filter])) $filter = 'top_rated';

$stats = getListeningNoteStats();
$songs = [];
try {
    $songs = getListeningNoteSongs($filter, getListDefaultLimit());
} catch (Throwable $e) {
    if (function_exists('writeAppLog')) writeAppLog('error', 'note_error', 'Listening notes page failed', ['filter' => $filter, 'error' => $e->getMessage()]);
}

renderHeader('감상 노트', 'listening_notes');
?>
<div class="listening-notes-page">
    <div class="page-title-section">
        <h1 class="page-main-title">감상 노트</h1>
        <p class="page-subtitle">가사, 개인 메모, 별점을 기준으로 음악 감상을 정리합니다.</p>
    </div>

    <?php renderStatGrid([
        ['label' => '별점 있는 곡', 'value' => number_format($stats['rated'])],
        ['label' => '5점 곡', 'value' => number_format($stats['five_star'])],
        ['label' => '메모 있는 곡', 'value' => number_format($stats['notes'])],
        ['label' => '가사 있는 곡', 'value' => number_format($stats['lyrics'])],
        ['label' => '미평가 곡', 'value' => number_format($stats['unrated'])],
    ]); ?>

    <section class="home-section">
        <div class="filter-tabs">
            <?php foreach ($filters as $key => $label): ?>
                <a class="filter-tab <?php echo $filter === $key ? 'active' : ''; ?>" href="listening_notes.php?filter=<?php echo h($key); ?>"><?php echo h($label); ?></a>
            <?php endforeach; ?>
        </div>
    </section>

    <section class="home-section">
        <div class="section-heading-row">
            <h2 class="section-title"><?php echo h($filters[$filter]); ?></h2>
            <span class="settings-muted"><?php echo number_format(count($songs)); ?>곡</span>
        </div>
        <?php if (empty($songs)): ?>
            <p class="empty-text">표시할 곡이 없습니다.</p>
        <?php else: ?>
            <div class="listening-note-list">
                <?php foreach ($songs as $song): ?>
                    <article class="listening-note-row song-item"
                             data-id="<?php echo h($song['id']); ?>"
                             data-title="<?php echo h($song['title']); ?>"
                             data-artist="<?php echo h(artistName($song['artist'] ?? '')); ?>"
                             data-album="<?php echo h(albumName($song['album'] ?? '')); ?>"
                             data-duration="<?php echo h($song['duration'] ?? 0); ?>"
                             data-liked="<?php echo !empty($song['is_liked']) ? '1' : '0'; ?>">
                        <button type="button" class="play-row-btn compact-play" title="재생"><i class="fas fa-play"></i></button>
                        <img src="cover.php?id=<?php echo h($song['id']); ?>" alt="" class="compact-cover" loading="lazy" width="56" height="56" onerror="this.onerror=null;this.src='assets/img/default-cover.svg';">
                        <div class="listening-note-main">
                            <a href="song.php?id=<?php echo h($song['id']); ?>" class="compact-title"><?php echo h($song['title']); ?></a>
                            <span><?php echo h(artistName($song['artist'] ?? '')); ?> / <?php echo h(albumName($song['album'] ?? '')); ?></span>
                            <div class="song-list-badges">
                                <?php if (!empty($song['has_note'])): ?><span class="song-note-badge"><i class="fa-regular fa-note-sticky"></i> 메모</span><?php endif; ?>
                                <a class="song-note-badge" href="lyrics_edit.php?song_id=<?php echo h($song['id']); ?>">가사</a>
                            </div>
                        </div>
                        <div class="listening-note-rating">
                            <?php renderRatingStars((int)($song['user_rating'] ?? 0), $song['id'], true); ?>
                        </div>
                    </article>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </section>
</div>
<?php renderFooter(); ?>
