<?php
// =============================================================
//  GET /api/v1/rotations/student_anomalies
//
//  System-wide scan for overdue/stalled clinical-student rotations —
//  FR17's anomaly detection for the clinical side, surfaced read-only for
//  the mobile Admin/Chief Preceptor oversight view. Calls
//  detectClinicalAnomalies() straight out of
//  app/ai_clinical_rotation_engine.php (pure read, no writes).
//
//  Note: this file and app/ai_rotation_engine.php (nurse_anomalies.php's
//  equivalent) declare functions with the SAME names — never
//  require_once both in one request. See nurse_anomalies.php's comment.
// =============================================================

require_once __DIR__ . '/../_bootstrap.php';
require_once __DIR__ . '/../../../app/ai_clinical_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' => detectClinicalAnomalies($conn)]);
