<?php
// =============================================================
//  GET /api/v1/assessments/nurse_mine
//
//  The authenticated Rotation Nurse's own assessment records — same
//  per-unit 3-stage summary app/nurse/my_assessments.php shows.
//  Submitting/editing an assessment stays a preceptor/in-charge action on
//  the desktop app for now (a 21-criteria-per-stage rubric form is a lot
//  of surface to safely translate to mobile in one pass) — this is
//  read-only, matching FR20's "students/nurses view" half.
// =============================================================

require_once __DIR__ . '/../_bootstrap.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');
}

$stmt = $conn->prepare("
    SELECT
        na.id, na.overall_status, na.is_locked,
        na.stage1_status, na.stage1_assessor_name, na.stage1_submitted_at,
        na.s1_total_score, na.s1_max_possible, na.s1_percentage, na.s1_grade, na.stage1_comments,
        na.stage2_status, na.stage2_assessor_name, na.stage2_submitted_at,
        na.s2_total_score, na.s2_max_possible, na.s2_percentage, na.s2_grade, na.stage2_comments,
        na.stage3_status, na.stage3_assessor_name, na.stage3_submitted_at,
        na.s3_total_score, na.s3_max_possible, na.s3_percentage, na.s3_grade, na.stage3_comments,
        na.final_percentage, na.final_grade, na.final_verdict,
        na.nurse_rotation_schedule_id AS schedule_id,
        nrs.unit_name, nrs.department_name, nrs.start_week, nrs.end_week, nrs.duration_weeks
    FROM nurse_assessments na
    JOIN nurse_rotation_schedules nrs ON na.nurse_rotation_schedule_id = nrs.id
    WHERE na.rotation_nurse_id = ?
    ORDER BY na.created_at DESC
");
$id = $account->getId();
$stmt->bind_param('i', $id);
$stmt->execute();
$assessments = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

Api::success(['assessments' => $assessments]);
