<?php
$host = 'localhost';
$db   = 'lms';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];

try {
    $pdo = new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
    die("DB Connection failed: " . $e->getMessage());
}

// --- Global Academic Year Logic ---
try {
    // Fetch the ID and Name of the year marked as 'is_current'
    $yearStmt = $pdo->query("SELECT id, year_name FROM academic_years WHERE is_current = 1 LIMIT 1");
    $currentYearData = $yearStmt->fetch(PDO::FETCH_ASSOC);

    if ($currentYearData) {
        // Define global constants
        define('APP_YEAR_ID', $currentYearData['id']);
        define('APP_YEAR_NAME', $currentYearData['year_name']);
    } else {
        // Fallback if no year is marked as current
        define('APP_YEAR_ID', 0);
        define('APP_YEAR_NAME', 'No Active Year');
    }
} catch (PDOException $e) {
    // Handle error if table doesn't exist yet
    define('APP_YEAR_ID', 0);
    define('APP_YEAR_NAME', 'Error');
}

// Ensure $pdo is available from your config
$stmt_count = $pdo->prepare("SELECT COUNT(id) as total FROM enrollments WHERE academic_year_id = ?");
$stmt_count->execute([APP_YEAR_ID]); // Using your global year constant
$total_students = $stmt_count->fetchColumn() ?: 0;

?>
