<?php
include('../includes/db.php');

header('Content-Type: application/json');

if (isset($_GET['exam_id'])) {
    $exam_id = intval($_GET['exam_id']);
    
    // First, check if exam exists
    $examQuery = "SELECT exm_year_ad, exm_year FROM examination_type WHERE id = ?";
    $stmtExam = $conn->prepare($examQuery);
    $stmtExam->bind_param("i", $exam_id);
    $stmtExam->execute();
    $examResult = $stmtExam->get_result();
    
    if ($examResult->num_rows === 0) {
        echo json_encode(["error" => "Exam not found"]);
        exit;
    }
    
    $examData = $examResult->fetch_assoc();
    $examYearAD = $examData['exm_year_ad'];  // This is integer like 2024
    $examYearBS = $examData['exm_year'];     // This might be like 2081
    
    // Try to find classes by matching academic year
    // First try with exm_year_ad
    $query1 = "SELECT DISTINCT c.class_id, c.class as class_name
              FROM student_class_section_data scsd
              INNER JOIN classes c ON scsd.class_id = c.class_id
              WHERE scsd.academic_year = ?
                AND scsd.status = 'active'
                AND c.status = 1
              ORDER BY c.class_numeric";
    
    $stmt1 = $conn->prepare($query1);
    $stmt1->bind_param("s", $examYearAD);
    $stmt1->execute();
    $result1 = $stmt1->get_result();
    
    $classes = [];
    if ($result1->num_rows > 0) {
        while ($row = $result1->fetch_assoc()) {
            $classes[] = [
                'class_id' => $row['class_id'],
                'class_name' => $row['class_name']
            ];
        }
    } else {
        // If not found with exm_year_ad, try with exm_year
        $query2 = "SELECT DISTINCT c.class_id, c.class as class_name
                  FROM student_class_section_data scsd
                  INNER JOIN classes c ON scsd.class_id = c.class_id
                  WHERE scsd.academic_year = ?
                    AND scsd.status = 'active'
                    AND c.status = 1
                  ORDER BY c.class_numeric";
        
        $stmt2 = $conn->prepare($query2);
        $stmt2->bind_param("s", $examYearBS);
        $stmt2->execute();
        $result2 = $stmt2->get_result();
        
        if ($result2->num_rows > 0) {
            while ($row = $result2->fetch_assoc()) {
                $classes[] = [
                    'class_id' => $row['class_id'],
                    'class_name' => $row['class_name']
                ];
            }
        } else {
            // If still no classes, get all active classes
            $query3 = "SELECT class_id, class as class_name 
                      FROM classes 
                      WHERE status = 1 
                      ORDER BY class_numeric";
            
            $result3 = $conn->query($query3);
            
            if ($result3->num_rows > 0) {
                while ($row = $result3->fetch_assoc()) {
                    $classes[] = [
                        'class_id' => $row['class_id'],
                        'class_name' => $row['class_name']
                    ];
                }
            }
        }
        
        if (isset($stmt2)) $stmt2->close();
    }
    
    echo json_encode($classes);
    
    $stmt1->close();
    $stmtExam->close();
} else {
    echo json_encode([]);
}

$conn->close();
?>