<?php
require_once 'config.php'; // Adjust path as needed
header('Content-Type: application/json');

$class_id = isset($_GET['class_id']) ? intval($_GET['class_id']) : 0;
$subjects = [];

if ($class_id > 0 && isset($conn) && $conn instanceof mysqli) {
    // Fetch subjects assigned to this class from stream_subject_assignments
    $sql = "SELECT DISTINCT s.id, s.name FROM subjects s
            JOIN stream_subject_assignments ssa ON s.id = ssa.subject_id
            WHERE ssa.class_id = ? ORDER BY s.name ASC";
    if ($stmt = $conn->prepare($sql)) {
        $stmt->bind_param("i", $class_id);
        $stmt->execute();
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()) {
            $subjects[] = $row;
        }
        $stmt->close();
    }
    $conn->close();
}
echo json_encode($subjects);
?>