<?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';

$limit = settingInt('recommendation_limit', 8, 3, 12);
$sections = getPersonalizedRecommendationSections48($limit);
$flatRecommended = getPersonalizedRecommendedSongs48(settingInt('recommendation_limit', 12, 3, 50));
$favoriteGenres = getFavoriteGenres(8);
$favoriteArtists = getFavoriteArtists(8);

renderHeader('나를 위한 추천', 'recommend');
?>
<div class="recommend-page recommendation-v48">
    <div class="page-title-section recommendation-hero">
        <div>
            <h1 class="page-main-title">나를 위한 추천</h1>
            <p class="page-subtitle">최근 재생, 좋아요, 장르, 아티스트, 별점 데이터를 바탕으로 지금 들을 만한 곡을 가볍게 골랐습니다.</p>
        </div>
        <div class="recommendation-hero-actions">
            <button type="button" class="btn btn-primary btn-play-all"><i class="fas fa-play"></i> 추천 전체 재생</button>
            <button type="button" class="btn btn-secondary btn-shuffle-all"><i class="fas fa-shuffle"></i> 셔플 재생</button>
            <button type="button" class="btn btn-secondary btn-add-page-next"><i class="fas fa-forward-step"></i> 다음에 재생</button>
            <button type="button" class="btn btn-secondary btn-add-page-to-queue"><i class="fas fa-list"></i> 대기열에 추가</button>
        </div>
    </div>

    <?php if (empty($sections)): ?>
        <section class="home-section recommendation-empty">
            <i class="fa-solid fa-wand-magic-sparkles"></i>
            <h2>아직 추천할 만큼의 청취 기록이 부족합니다.</h2>
            <p>몇 곡을 재생하거나 좋아요를 누르면 취향 기반 추천이 더 좋아집니다.</p>
            <div class="empty-state-actions">
                <a class="btn btn-primary" href="new.php"><i class="fas fa-plus"></i> 최근 추가 곡 듣기</a>
                <a class="btn btn-secondary" href="popular.php"><i class="fas fa-fire"></i> 많이 들은 곡 보기</a>
            </div>
        </section>
    <?php else: ?>
        <div class="recommendation-section-stack">
            <?php foreach ($sections as $section): ?>
                <section class="home-section recommendation-section" data-recommendation-section="<?php echo h($section['key']); ?>">
                    <div class="section-heading-row">
                        <div>
                            <h2 class="section-title"><?php echo h($section['title']); ?></h2>
                            <p class="section-description"><?php echo h($section['description']); ?></p>
                        </div>
                        <?php if (!empty($section['more_url'])): ?>
                            <a class="section-more-link" href="<?php echo h($section['more_url']); ?>">더보기</a>
                        <?php endif; ?>
                    </div>
                    <div class="compact-song-grid recommendation-song-grid">
                        <?php foreach ($section['songs'] as $song): ?>
                            <?php $isLiked = !empty($song['is_liked']); ?>
                            <div class="compact-song-card 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 $isLiked ? '1' : '0'; ?>">
                                <button type="button" class="play-row-btn compact-play" title="재생" aria-label="재생"><i class="fas fa-play"></i></button>
                                <img src="<?php echo h(coverUrl($song)); ?>" alt="" class="compact-cover" loading="lazy" width="52" height="52" onerror="this.onerror=null;this.src='assets/img/default-cover.svg';">
                                <div class="compact-song-meta">
                                    <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'] ?? '')); ?></span>
                                    <small class="recommend-reason"><?php echo h($song['recommend_reason'] ?? '추천 곡'); ?></small>
                                </div>
                                <div class="compact-song-actions" aria-label="곡 작업">
                                    <button type="button" class="like-btn <?php echo $isLiked ? 'liked' : ''; ?>" title="<?php echo $isLiked ? '좋아요 취소' : '좋아요'; ?>" aria-label="<?php echo $isLiked ? '좋아요 취소' : '좋아요'; ?>">
                                        <i class="<?php echo $isLiked ? 'fas fa-heart' : 'far fa-heart'; ?>"></i>
                                    </button>
                                    <button type="button" class="add-next-btn" title="다음에 재생" aria-label="다음에 재생"><i class="fas fa-forward-step"></i></button>
                                    <button type="button" class="add-playlist-btn" title="플레이리스트에 추가" aria-label="플레이리스트에 추가"><i class="fas fa-plus"></i></button>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </section>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>

    <section class="home-section">
        <div class="section-heading-row">
            <h2 class="section-title">취향 요약</h2>
            <a class="section-more-link" href="stats.php">청취 리포트</a>
        </div>
        <div class="preference-pill-grid">
            <?php foreach ($favoriteGenres as $genre): ?>
                <a class="preference-pill" href="genre.php?name=<?php echo urlencode($genre['genre']); ?>">
                    <i class="fa-solid fa-tag"></i>
                    <strong><?php echo h($genre['genre']); ?></strong>
                    <span><?php echo number_format((int)$genre['score']); ?>회 기반</span>
                </a>
            <?php endforeach; ?>
            <?php foreach ($favoriteArtists as $artist): ?>
                <a class="preference-pill" href="artist.php?name=<?php echo urlencode($artist['artist']); ?>">
                    <i class="fa-solid fa-user"></i>
                    <strong><?php echo h($artist['artist']); ?></strong>
                    <span><?php echo number_format((int)$artist['score']); ?>회 기반</span>
                </a>
            <?php endforeach; ?>
            <?php if (empty($favoriteGenres) && empty($favoriteArtists)): ?>
                <div class="empty-action-state">
                    <i class="fa-solid fa-music"></i>
                    <p>취향 요약을 만들 청취 기록이 아직 부족합니다.</p>
                </div>
            <?php endif; ?>
        </div>
    </section>

    <?php if (!empty($flatRecommended)): ?>
        <?php renderMiniSongSection('추천 모아보기', $flatRecommended, null); ?>
    <?php endif; ?>
</div>
<?php renderFooter(); ?>
