<?php
header('Content-Type: application/json');
require_once '../../config/config.php';

try {
    $allocationId = $_GET['allocation_id'] ?? null;
    
    if (!$allocationId) {
        echo json_encode([
            'status' => 'error',
            'message' => 'Allocation ID is required'
        ]);
        exit;
    }
    
    // Get hall allocation details
    $allocationQuery = "
        SELECT 
            ha.allocation_id,
            ha.num_students,
            h.hall_name,
            h.block_name,
            et.exam_date,
            et.session,
            et.start_time,
            et.end_time,
            s.subject_code,
            s.subject_name,
            e.exam_name,
            e.exam_id
        FROM hall_allocations ha
        JOIN halls h ON ha.hall_id = h.hall_id
        JOIN exam_timetable et ON ha.timetable_id = et.timetable_id
        JOIN exam_subjects es ON et.exam_subject_id = es.exam_subject_id
        JOIN subjects s ON es.subject_id = s.subject_id
        JOIN exams e ON es.exam_id = e.exam_id
        WHERE ha.allocation_id = ?
    ";
    
    $allocationStmt = $pdo->prepare($allocationQuery);
    $allocationStmt->execute([$allocationId]);
    $allocation = $allocationStmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$allocation) {
        echo json_encode([
            'status' => 'error',
            'message' => 'Hall allocation not found'
        ]);
        exit;
    }
    
    // Get students with seating and attendance status
    $studentsQuery = "
        SELECT 
            ss.seat_number,
            ss.registration_id,
            st.student_id,
            CONCAT(st.first_name, ' ', st.last_name) as student_name,
            st.university_rollno,
            c.course_name,
            COALESCE(ea.attendance_status, 'Unmarked') as attendance_status,
            ea.marked_at,
            ea.is_late,
            ea.remarks,
            ha.timetable_id
        FROM student_seating ss
        JOIN student_registrations sr ON ss.registration_id = sr.registration_id
        JOIN students st ON sr.student_id = st.student_id
        JOIN exam_subjects es ON sr.exam_subject_id = es.exam_subject_id
        JOIN subjects s ON es.subject_id = s.subject_id
        JOIN courses c ON s.course_id = c.course_id
        JOIN hall_allocations ha ON ss.allocation_id = ha.allocation_id
        LEFT JOIN tbl_exam_attendance ea ON ss.registration_id = ea.registration_id 
            AND ha.timetable_id = ea.timetable_id
        WHERE ss.allocation_id = ?
        ORDER BY ss.seat_number
    ";
    
    $studentsStmt = $pdo->prepare($studentsQuery);
    $studentsStmt->execute([$allocationId]);
    $students = $studentsStmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Calculate statistics
    $totalStudents = count($students);
    $presentCount = count(array_filter($students, function($s) { return $s['attendance_status'] === 'Present'; }));
    $absentCount = count(array_filter($students, function($s) { return $s['attendance_status'] === 'Absent'; }));
    $unmarkedCount = count(array_filter($students, function($s) { return $s['attendance_status'] === 'Unmarked'; }));
    
    echo json_encode([
        'status' => 'success',
        'data' => [
            'allocation' => $allocation,
            'students' => $students,
            'statistics' => [
                'total_students' => $totalStudents,
                'present_count' => $presentCount,
                'absent_count' => $absentCount,
                'unmarked_count' => $unmarkedCount
            ]
        ]
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'status' => 'error',
        'message' => 'Error: ' . $e->getMessage()
    ]);
}
?>
