<?php
session_start();
require_once '../../config/database.php';

header('Content-Type: application/json');

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'message' => 'Authentication required']);
    exit();
}

try {
    // Get academic sessions
    $stmt = $pdo->query("SELECT id, session_name FROM academic_sessions WHERE status = 'active' ORDER BY is_current DESC, start_date DESC");
    $sessions = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode(['success' => true, 'sessions' => $sessions]);
    
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Error fetching sessions: ' . $e->getMessage()]);
}
?>