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

$summary = getListeningSummary();
$trend7 = getListeningTrend(7);
$trend30 = getListeningTrend(30);
$topSongs = getTopSongsByHistory(10);
$topArtists = getTopFieldByHistory('artist', 10);
$topAlbums = getTopFieldByHistory('album', 10);
$topGenres = getTopFieldByHistory('genre', 10);
$recentFrequent = getRecentlyFrequentSongs(10);
$hourPattern = getPatternStats('hour');
$weekdayPattern = getPatternStats('weekday');
$recentAddedTop = getRecentAddedTopSongs(10);
$likedTop = getLikedTopSongs(10);
$noteStats = getListeningNoteStats();
$topRatedSongs = getListeningNoteSongs('top_rated', 10);
$unratedPopular = getListeningNoteSongs('popular_no_notes', 10);
$hasHistory = $summary['total'] > 0;

renderHeader('청취 리포트', 'stats');
?>
<div class="stats-page">
    <div class="page-title-section">
        <h1 class="page-main-title">청취 리포트</h1>
        <p class="page-subtitle">최근 청취 흐름과 자주 듣는 음악을 한눈에 봅니다.</p>
    </div>

    <?php renderStatGrid([
        ['label' => '오늘 들은 곡', 'value' => number_format($summary['today'])],
        ['label' => '최근 7일', 'value' => number_format($summary['week'])],
        ['label' => '최근 30일', 'value' => number_format($summary['month'])],
        ['label' => '전체 재생 기록', 'value' => number_format($summary['total'])],
    ]); ?>

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

    <?php if (!$hasHistory): ?>
        <section class="home-section">
            <h2 class="section-title">아직 청취 기록이 없습니다.</h2>
            <p class="empty-text">음악을 재생하면 오늘의 요약, TOP 곡, 시간대별 패턴이 이곳에 쌓입니다.</p>
        </section>
    <?php endif; ?>

    <div class="stats-grid">
        <section class="home-section stats-card-wide">
            <div class="section-heading-row"><h2 class="section-title">최근 7일 청취 추이</h2></div>
            <?php renderMiniBarChart($trend7); ?>
        </section>
        <section class="home-section stats-card-wide">
            <div class="section-heading-row"><h2 class="section-title">최근 30일 청취 추이</h2></div>
            <?php renderMiniBarChart($trend30); ?>
        </section>
        <section class="home-section">
            <div class="section-heading-row"><h2 class="section-title">많이 들은 아티스트 TOP 10</h2></div>
            <?php renderMiniBarChart($topArtists, 'name', 'plays'); ?>
        </section>
        <section class="home-section">
            <div class="section-heading-row"><h2 class="section-title">많이 들은 앨범 TOP 10</h2></div>
            <?php renderMiniBarChart($topAlbums, 'name', 'plays'); ?>
        </section>
        <section class="home-section">
            <div class="section-heading-row"><h2 class="section-title">많이 들은 장르 TOP 10</h2></div>
            <?php renderMiniBarChart($topGenres, 'name', 'plays'); ?>
        </section>
        <section class="home-section">
            <div class="section-heading-row"><h2 class="section-title">시간대별 청취 패턴</h2></div>
            <?php renderMiniBarChart($hourPattern); ?>
        </section>
        <section class="home-section">
            <div class="section-heading-row"><h2 class="section-title">요일별 청취 패턴</h2></div>
            <?php renderMiniBarChart($weekdayPattern); ?>
        </section>
    </div>

    <div class="home-section-grid">
        <?php renderMiniSongSection('가장 많이 들은 곡 TOP 10', $topSongs, 'popular.php'); ?>
        <?php renderMiniSongSection('최근 자주 들은 곡', $recentFrequent, 'recent.php'); ?>
        <?php renderMiniSongSection('최근 추가곡 중 많이 들은 곡', $recentAddedTop, 'new.php'); ?>
        <?php renderMiniSongSection('좋아요한 곡 중 많이 들은 곡', $likedTop, 'liked.php'); ?>
        <?php renderMiniSongSection('별점 높은 곡 중 다시 듣기', $topRatedSongs, 'listening_notes.php?filter=top_rated'); ?>
        <?php renderMiniSongSection('많이 들었지만 메모 없는 곡', $unratedPopular, 'listening_notes.php?filter=popular_no_notes'); ?>
    </div>
</div>
<?php renderFooter(); ?>
