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

$type = preg_replace('/[^a-z0-9_]/', '', (string)($_GET['type'] ?? 'recent_top'));
$definitions = getAutoPlaylistDefinitions();
if (!isset($definitions[$type])) {
    $type = 'recent_top';
}
$definition = $definitions[$type];
$songs = getAutoPlaylistSongs($type, 50);
$status = (string)($_GET['status'] ?? '');
$message = trim((string)($_GET['message'] ?? ''));
$defaultPlaylistName = $definition['title'] . ' ' . date('Y-m-d');

renderHeader($definition['title'], 'auto_playlists');
?>
<div class="auto-playlist-page">
    <div class="auto-playlist-detail-hero">
        <div class="auto-playlist-hero-copy">
            <a href="auto_playlists.php" class="auto-playlist-back-link"><i class="fa-solid fa-arrow-left"></i> 자동 플레이리스트</a>
            <span class="auto-playlist-kicker">자동 믹스</span>
            <h1 class="page-main-title"><?php echo h($definition['title']); ?></h1>
            <p class="page-subtitle"><?php echo h($definition['description']); ?></p>
        </div>
        <div class="auto-playlist-hero-meta" aria-label="자동 플레이리스트 요약">
            <span><strong><?php echo number_format(count($songs)); ?></strong>곡</span>
            <span>최대 50곡 표시</span>
        </div>
    </div>

    <?php if ($message !== ''): ?>
        <div class="alert-box <?php echo $status === 'error' ? 'alert-danger' : 'alert-success'; ?>">
            <div class="alert-icon"><i class="fas <?php echo $status === 'error' ? 'fa-exclamation-triangle' : 'fa-check-circle'; ?>"></i></div>
            <div class="alert-content"><p><?php echo h($message); ?></p></div>
        </div>
    <?php endif; ?>

    <?php if (empty($songs)): ?>
        <section class="home-section auto-playlist-empty-state">
            <i class="fa-solid fa-wand-magic-sparkles"></i>
            <h2>표시할 곡이 아직 없습니다.</h2>
            <p class="empty-text">청취 기록, 좋아요, 별점 또는 태그 데이터가 쌓이면 이 믹스가 자동으로 채워집니다.</p>
            <a href="auto_playlists.php" class="btn btn-secondary"><i class="fa-solid fa-arrow-left"></i> 목록으로</a>
        </section>
    <?php else: ?>
        <section class="home-section auto-playlist-action-panel">
            <div class="section-heading-row">
                <div>
                    <h2 class="section-title">이 믹스 활용하기</h2>
                    <p class="section-subtitle">재생하거나 일반 플레이리스트로 저장할 수 있습니다.</p>
                </div>
                <a class="section-more-link" href="auto_playlists.php">다른 자동 플레이리스트</a>
            </div>
            <div class="auto-playlist-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-to-queue"><i class="fas fa-list"></i> 대기열에 추가</button>
            </div>
            <?php if (isLoggedIn()): ?>
                <form class="auto-save-form" action="auto_playlist_save.php" method="POST" data-no-pjax>
                    <?php echo csrfInput(); ?>
                    <input type="hidden" name="type" value="<?php echo h($type); ?>">
                    <input type="hidden" name="limit" value="<?php echo h(min(count($songs), 100)); ?>">
                    <label for="playlist_name">일반 플레이리스트로 저장</label>
                    <div class="auto-save-row">
                        <input type="text" id="playlist_name" name="name" class="form-control" value="<?php echo h($defaultPlaylistName); ?>" maxlength="100" required>
                        <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> 저장</button>
                    </div>
                </form>
            <?php endif; ?>
        </section>
        <section class="auto-playlist-song-section" aria-label="자동 플레이리스트 곡 목록">
            <div class="section-heading-row">
                <h2 class="section-title">곡 목록</h2>
                <span class="section-count"><?php echo number_format(count($songs)); ?>곡</span>
            </div>
            <?php renderSongList($songs, ['show_play_actions' => false]); ?>
        </section>
    <?php endif; ?>
</div>
<?php renderFooter(); ?>
