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

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

echo "=== Phase 24 Climate Resilience Verification ==="

fail() { echo "FAIL: $1" >&2; exit 1; }
ok()   { echo "OK: $1"; }

[[ -f VigilCare.Simulator/Scenarios/List/ward-outage-reconnect-01.json ]] \
  || fail "Missing ward-outage-reconnect-01.json"
ok "ward-outage-reconnect-01.json"

[[ -f docs/resilience/climate/README.md ]] \
  || fail "Missing docs/resilience/climate/README.md"
ok "resilience README"

for n in 20 21 22 23 24; do
  [[ -f "docs/plans/phase-${n}-plan.md" ]] || fail "Missing phase-${n}-plan.md"
done
ok "phase plans 20-24"

dotnet build VigilCare.ClinicalContracts >/dev/null \
  && ok "ClinicalContracts builds" \
  || echo "WARN: ClinicalContracts build failed"

dotnet build VigilCareClinical.sln >/dev/null \
  && ok "Solution builds" \
  || echo "WARN: Solution build failed"

if dotnet test VigilCareClinicalAPI.Tests \
  --filter "FullyQualifiedName~ClimateResilience" --no-build 2>/dev/null; then
  ok "ClimateResilience tests pass"
else
  echo "WARN: ClimateResilience tests not found or failed"
fi

grep -qi "ward gateway" docs/interview-questions.md \
  || fail "Interview questions missing ward gateway section"
ok "interview questions #29-32"

[[ -x scripts/demo-network-partition.sh ]] \
  || fail "Missing or non-executable demo-network-partition.sh"
ok "partition demo script"

# Count experiment artifact sets (expect 6 experiments × 4 files = 24)
ARTIFACT_COUNT=$(find docs/resilience/climate -maxdepth 1 -name '2026-*' 2>/dev/null | wc -l)
if [[ "$ARTIFACT_COUNT" -lt 4 ]]; then
  echo "WARN: Only $ARTIFACT_COUNT chaos artifacts found (expect 24 for full completion)"
else
  ok "$ARTIFACT_COUNT chaos experiment artifacts"
fi

echo ""
echo "Phase 24 verification passed."