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

try {
    // We select id, name, and is_active status
    // Ordering by session_name DESC puts the newest years at the top
    $stmt = $pdo->query("SELECT id, year_name, is_current FROM academic_years ORDER BY year_name DESC");
    $years = $stmt->fetchAll(PDO::FETCH_ASSOC);

    if ($years) {
        echo json_encode($years);
    } else {
        // Return empty array if no years exist yet
        echo json_encode([]);
    }

} catch (PDOException $e) {
    // Return error message if database query fails
    http_response_code(500);
    echo json_encode(["status" => "error", "message" => $e->getMessage()]);
}