<?php

declare(strict_types=1);

require_once __DIR__ . '/Database.php';

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

try {

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

    if ($departmentId <= 0) {
        echo json_encode([]);
        exit;
    }

    $pdo = Database::connection();

    $stmt = $pdo->prepare("
        SELECT id, name
        FROM programs
        WHERE department_id = ?
        AND status = 'ACTIVE'
        ORDER BY name ASC
    ");

    $stmt->execute([$departmentId]);

    echo json_encode(
        $stmt->fetchAll()
    );

} catch (Throwable $e) {

    http_response_code(500);

    echo json_encode([
        'success' => false,
        'message' => 'Unable to load options.'
    ]);
}