<?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="page-title-section">
        <h1 class="page-main-title"><?php echo h($definition['title']); ?></h1>
        <p class="page-subtitle"><?php echo h($definition['description']); ?> · <?php echo number_format(count($songs)); ?>곡</p>
    </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">
            <p class="empty-text">이 자동 플레이리스트에 표시할 곡이 아직 없습니다.</p>
            <a href="auto_playlists.php" class="btn btn-secondary">목록으로</a>
        </section>
    <?php else: ?>
        <section class="home-section auto-playlist-action-panel">
            <div class="section-heading-row">
                <h2 class="section-title">이 믹스 활용하기</h2>
                <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>
        <?php renderSongList($songs, ['show_play_actions' => false]); ?>
    <?php endif; ?>
</div>
<?php renderFooter(); ?>
