<?php
// =============================================================
//  GET /api/v1/rotations/nurse_anomalies
//
//  System-wide scan for overdue/stalled/overcrowded nurse rotations —
//  FR17's anomaly detection, surfaced read-only for the mobile Admin/
//  Chief Preceptor oversight view. Calls detectScheduleAnomalies()
//  straight out of app/ai_rotation_engine.php — that function is a pure
//  read (no writes, no Gemini call), so it's reused as-is rather than
//  duplicated.
//
//  Note: app/ai_clinical_rotation_engine.php (student_anomalies.php's
//  equivalent) declares functions with the SAME names as this file
//  (hasInternetConnection(), getClinicalSequenceRules()) — the two engine
//  files can never both be require_once'd in one request. Each mobile
//  endpoint loads exactly one.
// =============================================================

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

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

$account = api_authenticate();
api_require_staff_role($account, ['Admin', 'Chief_Preceptor']);

Api::success(['anomalies' => detectScheduleAnomalies($conn)]);
