<?php
require_once '../includes/auth.php';
require_once '../includes/db.php';

if (!isAdmin()) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

$peserta_id = $_GET['peserta_id'] ?? 0;

if (!$peserta_id) {
    echo json_encode(['success' => false, 'message' => 'Peserta ID required']);
    exit;
}

try {
    $stmt = $pdo->prepare("
        SELECT 
            ps.peserta_id,
            p.nama as peserta_nama,
            ps.ujian_id,
            u.nama_ujian,
            ps.station_id,
            s.nama_station,
            ps.urutan,
            ps.status
        FROM peserta_station ps
        JOIN peserta p ON ps.peserta_id = p.id
        JOIN ujian u ON ps.ujian_id = u.id
        JOIN stations s ON ps.station_id = s.id
        WHERE ps.peserta_id = ?
        ORDER BY ps.urutan
    ");
    $stmt->execute([$peserta_id]);
    $assignments = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode([
        'success' => true,
        'assignments' => $assignments,
        'count' => count($assignments)
    ]);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
?>