<?php
header('Content-Type: application/json');
require_once 'config/database.php';

try {
    $class_id = isset($_GET['class_id']) ? (int)$_GET['class_id'] : 0;
    
    if ($class_id > 0) {
        // Fetch subjects for the given class
        $stmt = $pdo->prepare("
            SELECT s.id as subject_id, s.name, s.code
            FROM subjects s
            JOIN class_subjects cs ON s.id = cs.subject_id
            WHERE cs.class_id = ? AND s.status = 'active'
            ORDER BY s.name
        ");
        $stmt->execute([$class_id]);
        $subjects = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        echo json_encode([
            'success' => true,
            'subjects' => $subjects
        ]);
    } else {
        echo json_encode([
            'success' => false,
            'message' => 'Invalid class ID'
        ]);
    }
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => 'Error fetching subjects: ' . $e->getMessage()
    ]);
}
?>