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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BASE_URL="${BASE_URL:-http://localhost:5270}"

echo "=== Phase 31 verification ==="

dotnet test "${ROOT_DIR}/VigilCareClinicalAPI.Tests" \
  --filter "FullyQualifiedName~RbacTests" --no-restore

TOKEN=$(curl -sf -X POST "${BASE_URL}/api/v1/auth/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"nurse.demo","password":"DemoNurse1!"}' \
  | jq -r '.data.accessToken')

echo "Nurse token acquired"
curl -sf "${BASE_URL}/api/v1/patients" \
  -H "Authorization: Bearer ${TOKEN}" | jq -e '.success == true'

ADMIN_TOKEN=$(curl -sf -X POST "${BASE_URL}/api/v1/auth/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"admin.demo","password":"DemoAdmin1!"}' \
  | jq -r '.data.accessToken')

curl -sf "${BASE_URL}/api/v1/audit-logs" \
  -H "Authorization: Bearer ${ADMIN_TOKEN}" | jq -e '.success == true'

echo "Phase 31 verification complete."