<?php
// =============================================================
//  GET /api/v1/reference/departments
//
//  Read-only reference data — every authenticated role needs the
//  department list for filtering/display (e.g. a student's rotation
//  card showing which department a unit belongs to). Management
//  (create/edit/deactivate departments) stays on the desktop web app for
//  now, consistent with the thesis's own scope decision to keep heavy
//  admin configuration desktop-first (see the plan's Ch.4.6 note) —
//  app/departments.php / create_department.php / edit_department.php are
//  unchanged and remain the place that happens.
// =============================================================

require_once __DIR__ . '/../_bootstrap.php';

if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
    Api::error(405, 'MethodNotAllowed', 'Use GET');
}

api_authenticate();

$result = $conn->query("
    SELECT id, department_name, nurse_manager, preceptor, status
    FROM departments
    WHERE status = 'Active'
    ORDER BY department_name
");

Api::success(['departments' => $result->fetch_all(MYSQLI_ASSOC)]);
