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

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

echo "=== Phase 28 verification ==="

echo "1. Dashboard unit tests"
cd "${ROOT_DIR}/vigilcare-dashboard"
npm test

echo "2. Manual UI checks (requires running stack + dashboard dev server)"
BASE_URL="${BASE_URL:-http://localhost:5270}"
DASH_URL="${DASH_URL:-http://localhost:5173}"
ENCOUNTER_ID="${ENCOUNTER_ID:?Set ENCOUNTER_ID to an active encounter UUID}"

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

post_gcs 3 4 5
curl -sf "${BASE_URL}/api/v1/encounters/${ENCOUNTER_ID}/gcs" | jq -e '.data.totalScore == 12'
curl -sf "${BASE_URL}/api/v1/encounters/${ENCOUNTER_ID}/sofa" || echo "(SOFA 404 OK if labs not yet ingested)"

echo "Open ${DASH_URL}/encounters/${ENCOUNTER_ID} and verify:"
echo "  - ScoresPanel: NEWS2, GCS, SOFA, qSOFA Screen sections"
echo "  - GCS form: E=3 V=4 M=5 → total 12 Moderate"
echo "  - SofaScorePanel: 6-organ grid or Labs pending"
echo "  - SepsisBundlePanel: SOFA trigger label or qSOFA screen message"
echo "  - Alert Center: new alert type labels; no SIRS except legacy"

echo "Phase 28 verification complete."
