#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"

echo "=== Phase 25 verification ==="

echo "1. Integration tests"
dotnet test "${ROOT_DIR}/VigilCareClinicalAPI.Tests" \
  --filter "FullyQualifiedName~GcsScoring" \
  --no-restore

echo "2. Manual API checks (requires running stack)"
BASE_URL="${BASE_URL:-http://localhost:5270}"
ENCOUNTER_ID="${ENCOUNTER_ID:?Set ENCOUNTER_ID to an active encounter UUID}"

post_obs() {
  local code="$1" value="$2"
  curl -sf -X POST "${BASE_URL}/api/v1/encounters/${ENCOUNTER_ID}/observations" \
    -H "Content-Type: application/json" \
    -d "{\"observations\":[{\"observationCode\":\"${code}\",\"value\":${value},\"unit\":\"score\",\"source\":\"Manual\",\"recordedAt\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"}]}"
}

post_obs GCS_EYE 4
post_obs GCS_VERBAL 5
post_obs GCS_MOTOR 6
curl -sf "${BASE_URL}/api/v1/encounters/${ENCOUNTER_ID}/gcs" | jq -e '.data.totalScore == 15'

post_obs GCS_EYE 1
post_obs GCS_VERBAL 2
post_obs GCS_MOTOR 3
echo "Verify GCS_CRITICAL alert, NEWS2 consciousness=3, and qSOFA altered mentation in DB/UI"

echo "Phase 25 verification complete."