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

$pdo = getDBConnection();
$artist = normalizeMetaParam($_GET['name'] ?? $_GET['id'] ?? '');
$label = artistName($artist);
$params = [];
$artistWhere = metaWhereSql('s.artist', ':artist', $artist === '알 수 없는 아티스트' ? '' : $artist, $params);

$songs = fetchSongs("s.is_missing = 0 AND $artistWhere", $params, 's.album ASC, s.track_no ASC, s.title ASC');

$albumsStmt = $pdo->prepare("
    SELECT COALESCE(NULLIF(TRIM(s.album), ''), '알 수 없는 앨범') AS album_label,
           COUNT(*) AS song_count,
           COALESCE(SUM(s.play_count), 0) AS total_plays,
           MAX(s.id) AS cover_song_id,
           MAX(s.year) AS year
    FROM songs s
    WHERE s.is_missing = 0 AND $artistWhere
    GROUP BY album_label
    ORDER BY album_label ASC
");
$albumsStmt->execute($params);
$albums = $albumsStmt->fetchAll();

$likedCount = 0;
foreach ($songs as $song) {
    if (!empty($song['is_liked'])) $likedCount++;
}

renderHeader($label, 'artists');
?>
<div class="detail-page">
    <div class="detail-hero">
        <img src="<?php echo !empty($songs) ? 'cover.php?id=' . h($songs[0]['id']) : 'assets/img/default-cover.svg'; ?>" alt="" class="detail-cover" width="180" height="180" onerror="this.onerror=null;this.src='assets/img/default-cover.svg';">
        <div>
            <span class="detail-kicker">아티스트</span>
            <h1 class="page-main-title"><?php echo h($label); ?></h1>
            <?php renderStatGrid([
                ['label' => '곡 수', 'value' => number_format(count($songs))],
                ['label' => '앨범 수', 'value' => number_format(count($albums))],
                ['label' => '총 재생', 'value' => number_format(array_sum(array_column($songs, 'play_count')))],
                ['label' => '좋아요', 'value' => number_format($likedCount)],
            ]); ?>
        </div>
    </div>

    <?php if (!empty($songs)) renderPageActions(); ?>

    <section class="home-section">
        <div class="section-heading-row"><h2 class="section-title">앨범</h2></div>
        <div class="entity-grid small">
            <?php foreach ($albums as $album): ?>
                <a class="entity-card" href="album.php?album=<?php echo urlencode($album['album_label']); ?>&artist=<?php echo urlencode($label); ?>">
                    <img src="cover.php?id=<?php echo h($album['cover_song_id']); ?>" alt="" class="entity-cover" loading="lazy" width="96" height="96" onerror="this.onerror=null;this.src='assets/img/default-cover.svg';">
                    <div class="entity-info">
                        <h2><?php echo h($album['album_label']); ?></h2>
                        <p><?php echo number_format($album['song_count']); ?>곡<?php echo $album['year'] ? ' · ' . h($album['year']) : ''; ?></p>
                    </div>
                </a>
            <?php endforeach; ?>
        </div>
    </section>

    <section class="home-section">
        <div class="section-heading-row"><h2 class="section-title">전체 곡</h2></div>
        <?php renderSongList($songs, ['show_play_actions' => false]); ?>
    </section>
</div>
<?php renderFooter(); ?>
