<?php
header("Content-Type: application/json");
require("../../includes/config.php");

$id = $_GET['id'] ?? null;

if (!$id) {
    echo json_encode(["status" => false, "msg" => "Student ID is required"]);
    exit;
}

try {
    // REMOVED: st.stream_name and JOIN streams
    $sql = "SELECT s.*, e.roll_no, c.class_name, ay.year_name, sec.section_name, m.medium_name 
            FROM students s
            JOIN enrollments e ON s.id = e.student_id
            JOIN classes c ON e.class_id = c.id
            JOIN academic_years ay ON e.academic_year_id = ay.id
            LEFT JOIN sections sec ON e.section_id = sec.id
            LEFT JOIN mediums m ON e.medium_id = m.id
            WHERE s.id = :id AND e.academic_year_id = :year_id";
    
    $stmt = $pdo->prepare($sql);
    $stmt->execute([':id' => $id, ':year_id' => APP_YEAR_ID]);
    $student = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($student) {
        // Fallback for medium only
        $student['medium_name'] = $student['medium_name'] ?? 'N/A';
        
        echo json_encode(["status" => true, "data" => $student]);
    } else {
        echo json_encode(["status" => false, "msg" => "Student not found"]);
    }
} catch (Exception $e) {
    echo json_encode(["status" => false, "msg" => $e->getMessage()]);
}
?>