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

$class_id = (int)($_GET['class_id'] ?? 0);

if (!$class_id) {
    echo json_encode(['status' => 'error', 'message' => 'Class ID required']);
    exit;
}

try {
    // UPDATED LOGIC: 
    // Simply fetch all subjects for the given class.
    // Stream filtering is removed to ensure all subjects appear for the selected class.
    
    $query = "SELECT id, subject_name 
              FROM subjects 
              WHERE class_id = :class_id 
              ORDER BY subject_name ASC";

    $stmt = $pdo->prepare($query);
    $params = [':class_id' => $class_id];

    $stmt->execute($params);
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode(['status' => 'success', 'data' => $data]);

} catch (PDOException $e) {
    echo json_encode(['status' => 'error', 'message' => 'Database Error: ' . $e->getMessage()]);
}
?>