<?php
// =============================================================
//  GET /api/v1/rotations/nurse_mine
//
//  The authenticated Rotation Nurse's own full schedule — same data
//  app/nurse/my_schedule.php shows, via the shared
//  include/RotationScheduleHelpers.php helper.
// =============================================================

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

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

$account = api_authenticate();
if ($account->getTable() !== 'rotation_nurses') {
    Api::error(403, 'Forbidden', 'This endpoint is for Rotation Nurses only');
}

$schedules = getNurseScheduleWithLiveStatus($conn, $account->getId());

$total    = count($schedules);
$done     = count(array_filter($schedules, fn($s) => $s['live_status'] === 'Completed'));
$ongoing  = array_values(array_filter($schedules, fn($s) => $s['live_status'] === 'In_Progress'));
$upcoming = array_values(array_filter($schedules, fn($s) => $s['live_status'] === 'Upcoming'));

Api::success([
    'schedules' => $schedules,
    'summary'   => [
        'total'            => $total,
        'completed'        => $done,
        'progress_percent' => $total > 0 ? round(($done / $total) * 100) : 0,
        'current'          => $ongoing[0]  ?? null,
        'next'             => $upcoming[0] ?? null,
    ],
]);
