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

$id = filter_var($_POST['id'] ?? null, FILTER_VALIDATE_INT);

if (!$id) {
    echo json_encode(['status' => 'error', 'message' => 'Invalid ID provided.']);
    exit;
}

try {
    $pdo->beginTransaction();
    $pdo->query("UPDATE academic_years SET is_current = 0");
    
    $stmt = $pdo->prepare("UPDATE academic_years SET is_current = 1 WHERE id = ?");
    $stmt->execute([$id]);

    $pdo->commit();
    echo json_encode(['status' => 'success', 'message' => 'Current year updated!']);
} catch (Exception $e) {
    if ($pdo->inTransaction()) $pdo->rollBack();
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
?>