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

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

$exam_id = isset($_POST['exam_id']) ? intval($_POST['exam_id']) : 0;
$class_id = isset($_POST['class_id']) ? intval($_POST['class_id']) : 0;
$subject_id = isset($_POST['subject_id']) ? intval($_POST['subject_id']) : 0;
$section_ids = isset($_POST['section_id']) ? $_POST['section_id'] : [];

// Get exam details
$exam_query = "SELECT exam_type, exm_year FROM examination_type WHERE id = ?";
$stmt = $conn->prepare($exam_query);
$stmt->bind_param("i", $exam_id);
$stmt->execute();
$exam_result = $stmt->get_result();
$exam = $exam_result->fetch_assoc();

// Get class details
$class_query = "SELECT class FROM classes WHERE class_id = ?";
$stmt = $conn->prepare($class_query);
$stmt->bind_param("i", $class_id);
$stmt->execute();
$class_result = $stmt->get_result();
$class = $class_result->fetch_assoc();

// Get subject details
$subject_query = "SELECT subject_name, subject_code, 
                         full_marks_theory, full_marks_practical, full_marks_internal
                  FROM subjects WHERE id = ?";
$stmt = $conn->prepare($subject_query);
$stmt->bind_param("i", $subject_id);
$stmt->execute();
$subject_result = $stmt->get_result();
$subject = $subject_result->fetch_assoc();

// Get students
$student_query = "SELECT s.student_id, s.roll_no, 
                         CONCAT(s.first_name, ' ', s.last_name) as student_name
                  FROM students s
                  WHERE s.class_id = ? AND s.status = 1";
                  
if (!empty($section_ids)) {
    $placeholders = implode(',', array_fill(0, count($section_ids), '?'));
    $student_query .= " AND s.section_id IN ($placeholders)";
}

$student_query .= " ORDER BY s.roll_no";

$stmt = $conn->prepare($student_query);

if (!empty($section_ids)) {
    $params = array_merge([$class_id], $section_ids);
    $stmt->bind_param(str_repeat('i', count($params)), ...$params);
} else {
    $stmt->bind_param("i", $class_id);
}

$stmt->execute();
$student_result = $stmt->get_result();

$students = [];
while ($row = $student_result->fetch_assoc()) {
    $students[] = $row;
}

// Get section names
$sections = [];
if (!empty($section_ids)) {
    $section_query = "SELECT section_name FROM sections WHERE section_id IN (" . 
                    implode(',', array_fill(0, count($section_ids), '?')) . ")";
    $stmt = $conn->prepare($section_query);
    $stmt->bind_param(str_repeat('i', count($section_ids)), ...$section_ids);
    $stmt->execute();
    $section_result = $stmt->get_result();
    while ($row = $section_result->fetch_assoc()) {
        $sections[] = $row['section_name'];
    }
}

$response = [
    'status' => 'success',
    'data' => [
        'exam_name' => $exam['exam_type'] . ' - ' . $exam['exm_year'],
        'class_name' => $class['class'],
        'subject_name' => $subject['subject_name'] . ' (' . $subject['subject_code'] . ')',
        'sections' => !empty($sections) ? implode(', ', $sections) : 'All Sections',
        'total_students' => count($students),
        'has_theory' => $subject['full_marks_theory'] > 0,
        'has_practical' => $subject['full_marks_practical'] > 0,
        'has_internal' => $subject['full_marks_internal'] > 0,
        'full_marks_theory' => $subject['full_marks_theory'],
        'full_marks_practical' => $subject['full_marks_practical'],
        'full_marks_internal' => $subject['full_marks_internal'],
        'students' => $students
    ]
];

echo json_encode($response);
?>