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

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
PYTHON_BIN="${PYTHON:-python3}"

mkdir -p proof .crabbox-results

"$PYTHON_BIN" scripts/check_crabbox_ci_contract.py \
  --output .crabbox-results/crabbox-ci-contract.json

"$PYTHON_BIN" -m pytest \
  tests/test_agent_routing.py \
  tests/test_evaluate_agent_routing.py \
  tests/test_phase1b_end_to_end.py \
  tests/test_eval_parse.py \
  tests/test_contributor.py \
  tests/test_search.py \
  --junitxml=.crabbox-results/phase1b-pytest.xml

PHASE1B_AGENT_ROUTING_ENABLED=true \
  "$PYTHON_BIN" scripts/prove_phase1b_local.py \
  --output proof/phase1b-local-e2e-proof.json

"$PYTHON_BIN" - <<'PY'
import json
from pathlib import Path

proof_path = Path("proof/phase1b-local-e2e-proof.json")
proof = json.loads(proof_path.read_text())
contract = json.loads(Path(".crabbox-results/crabbox-ci-contract.json").read_text())
summary = {
    "ok": proof.get("ok") is True,
    "scope": proof.get("scope"),
    "schema_version": proof.get("schema_version"),
    "crabbox_ci_contract_ok": contract.get("ok") is True,
    "leo_route_contract_ok": contract.get("leo_route_contract", {}).get("ok") is True,
    "agents_seen": proof.get("agents_seen", []),
    "cases_total": proof.get("cases_total"),
    "succeeded": proof.get("succeeded"),
    "failed": proof.get("failed"),
}
if not summary["ok"]:
    raise SystemExit(f"phase1b proof failed: {summary}")
if len(summary["agents_seen"]) != 6:
    raise SystemExit(f"expected six agents, got {summary['agents_seen']}")
Path(".crabbox-results/phase1b-proof-summary.json").write_text(
    json.dumps(summary, indent=2, sort_keys=True) + "\n"
)
print(json.dumps(summary, indent=2, sort_keys=True))
PY
