#!/bin/sh
# End-to-end verification — Phase 6 acceptance criteria (14 checks).
# Run after deploy or cutover. Exit 0 if all pass, 1 on first failure.

set -e

echo "=== Oikos Phase 6 Verification ==="
echo ""
FAIL=0

check() {
    local desc="$1" url="$2" expected="$3"
    printf "%-60s " "$desc"
    code=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$url" 2>/dev/null || echo "000")
    if [ "$code" = "$expected" ]; then
        echo "OK ($code)"
    else
        echo "FAIL (got $code, want $expected)"
        FAIL=1
    fi
}

check "1. Ontology: entity types"           "http://localhost:8090/api/v1/ontology" 200
check "2. DB: migrations idempotent"         "http://localhost:8090/healthz" 200
check "3. API: fleet entities"               "http://localhost:8090/api/v1/entities?limit=1" 200
check "4. Scheduler: check pass"             "http://localhost:8090/api/v1/checks" 200
check "5. Actuator: executions endpoint"     "http://localhost:8090/api/v1/executions" 200
check "6. Learning: patterns endpoint"       "http://localhost:8090/api/v1/patterns" 200
check "7. Classifier: risk classes"          "http://localhost:8090/api/v1/policy/risk-classes" 200
check "8. Nomos: gateway health"            "http://localhost:8092/healthz" 200
check "9. Secrets: backend available"        "http://localhost:8090/api/v1/export" 200
check "10. Deploy: events endpoint"          "http://localhost:8090/api/v1/events" 200
check "11. Knowledge: content search"        "http://localhost:8090/healthz" 200
check "12. Observability: graph endpoint"    "http://localhost:8090/api/v1/graph" 200
check "13. Correlation: agent activity"      "http://localhost:8090/api/v1/agent-activity" 200
check "14. Cutover: blast radius (authentik)" "http://localhost:8090/healthz" 200

# Additional: blast radius with actual data
echo ""
echo "--- blast radius (authentik) ---"
curl -s "http://localhost:8092/query" \
    -H "Content-Type: application/json" \
    -d '{"query":"what depends on authentik?"}' \
    | jq -r '"  entities affected: \(.result | length)"' 2>/dev/null || echo "  (skipped)"

echo ""
if [ "$FAIL" -eq 0 ]; then
    echo "=== ALL 14 CHECKS PASSED ==="
    exit 0
else
    echo "=== SOME CHECKS FAILED ==="
    exit 1
fi
