<?php
// Ensure this path is correct based on your file structure
require_once('../../includes/config.php'); 

header('Content-Type: application/json');
ob_start(); // Trap any accidental PHP warnings

try {
    // 1. Get Session ID (Use GET param or Global Constant)
    $session_id = $_GET['academic_year_id'] ?? (defined('APP_YEAR_ID') ? APP_YEAR_ID : null);

    if (!$session_id) {
        throw new Exception("Academic Year ID is missing.");
    }

    // 2. Fetch Session Start Date
    $sessionQuery = $pdo->prepare("SELECT start_date FROM academic_years WHERE id = ?");
    $sessionQuery->execute([$session_id]);
    $session = $sessionQuery->fetch(PDO::FETCH_ASSOC);

    if (!$session) {
        throw new Exception("Session not found.");
    }

    $sessionStartDate = $session['start_date'] ?? date('Y-04-01');

    // 3. Output clean JSON
    ob_clean(); // Clear the trap
    echo json_encode([
        "status" => "success",
        "start_date" => $sessionStartDate
    ]);

} catch (Exception $e) {
    ob_clean();
    echo json_encode([
        "status" => "error",
        "message" => $e->getMessage()
    ]);
}
?>