<?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';
requireAdmin();

$pdo = getDBConnection();
$message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    requireCsrfToken();
    $name = trim((string)($_POST['name'] ?? ''));
    $description = trim((string)($_POST['description'] ?? ''));
    $displayScope = normalizePlaylistDisplayScope18($_POST['display_scope'] ?? 'private');
    
    if ($name === '') {
        $message = '플레이리스트 이름을 입력해 주세요.';
    } else {
        try {
            $stmt = $pdo->prepare("INSERT INTO playlists (name, description, display_scope) VALUES (:name, :description, :display_scope)");
            $stmt->execute([
                ':name' => function_exists('mb_substr') ? mb_substr($name, 0, 255, 'UTF-8') : substr($name, 0, 255),
                ':description' => $description !== '' ? $description : null,
                ':display_scope' => $displayScope,
            ]);
            
            // 성공 시 리스트 목록으로 리다이렉트
            header("Location: playlists.php");
            exit;
        } catch (Throwable $e) {
            if (function_exists('writeAppLog')) {
                writeAppLog('error', 'playlist_create_error', 'Playlist create failed', ['error' => $e->getMessage()]);
            }
            $message = '플레이리스트를 만들지 못했습니다.';
        }
    }
}

renderHeader('새 플레이리스트 생성', 'playlists');
?>

<div class="playlist-create-page">
    <div class="page-title-section">
        <h1 class="page-main-title">새 플레이리스트 만들기</h1>
        <p class="page-subtitle">나만의 독창적인 컬렉션을 위한 리스트를 생성합니다.</p>
    </div>

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

    <div class="scan-action-section" style="display: block; max-width: 600px;">
        <form action="playlist_create.php" method="POST">
            <div class="form-group">
                <label for="name">플레이리스트 이름 (필수)</label>
                <input type="text" name="name" id="name" class="form-control" placeholder="예: 운동할 때 듣는 팝송" required>
            </div>
            
            <div class="form-group">
                <label for="description">플레이리스트 설명 (선택)</label>
                <textarea name="description" id="description" class="form-control" placeholder="리스트에 대한 간단한 설명을 작성해 주세요."></textarea>
            </div>

            <div class="form-group">
                <label for="display_scope">표시 옵션</label>
                <select name="display_scope" id="display_scope" class="form-control">
                    <?php foreach (playlistDisplayScopeOptions18() as $scopeValue => $scopeLabel): ?>
                        <option value="<?php echo h($scopeValue); ?>"><?php echo h($scopeLabel); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            
            <div class="actions-row">
                <a href="playlists.php" class="btn btn-secondary">취소</a>
                <button type="submit" class="btn btn-primary">생성하기</button>
            </div>
        </form>
    </div>
</div>

<?php
renderFooter();
?>
