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

// Set JSON header
header('Content-Type: application/json; charset=UTF-8');

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    http_response_code(401);
    echo json_encode(['error' => 'Unauthorized']);
    exit();
}

$subject_id = $_GET['id'] ?? '';

if (!$subject_id) {
    http_response_code(400);
    echo json_encode(['error' => 'Subject ID required']);
    exit();
}

try {
    $stmt = $pdo->prepare("
        SELECT id, exam_date, start_time, end_time, room_number, invigilator, marks
        FROM exam_subjects
        WHERE id = ?
    ");
    $stmt->execute([$subject_id]);
    $subject = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$subject) {
        http_response_code(404);
        echo json_encode(['error' => 'Subject not found']);
        exit();
    }
    
    echo json_encode($subject);
    
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
}
?>