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

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
MODE="${DAARWIZZ_AWARENESS_MODE:-static}" # static | runtime
ROUTER_URL="${ROUTER_URL:-http://127.0.0.1:9102}"
REQUEST_TIMEOUT_SEC="${DAARWIZZ_AWARENESS_TIMEOUT_SEC:-35}"
OUT_JSON="${DAARWIZZ_AWARENESS_OUT_JSON:-/tmp/daarwizz_awareness_audit.json}"

PYTHON_BIN="python3"
if [[ -x "$ROOT/.venv-codex/bin/python" ]]; then
  PYTHON_BIN="$ROOT/.venv-codex/bin/python"
fi

echo "[INFO] checking DAARWIZZ awareness (mode=$MODE)"

"$PYTHON_BIN" - "$ROOT" "$MODE" "$ROUTER_URL" "$REQUEST_TIMEOUT_SEC" "$OUT_JSON" <<'PY'
import json
import re
import sys
import time
from pathlib import Path
from urllib import request as urlreq

import yaml

root = Path(sys.argv[1])
mode = sys.argv[2]
router = sys.argv[3].rstrip("/")
timeout_s = int(sys.argv[4])
out_json = Path(sys.argv[5])

registry = yaml.safe_load((root / "config" / "agent_registry.yml").read_text(encoding="utf-8"))
agents = [a for a in registry.get("agents", []) if a.get("class") == "top_level"]

results = []

if mode == "static":
    for idx, agent in enumerate(agents, 1):
        aid = agent["id"]
        prompt_file = agent.get("prompt_file", "")
        row = {"agent": aid, "status": "FAIL", "reason": "", "preview": ""}

        # DAARWIZZ itself can be defined without self-reference check.
        if aid == "daarwizz":
            row["status"] = "PASS"
            row["reason"] = "self_agent"
            results.append(row)
            print(f"[{idx:02d}/{len(agents)}] {aid}: PASS (self_agent)", flush=True)
            continue

        if not prompt_file:
            row["reason"] = "missing_prompt_file"
            results.append(row)
            print(f"[{idx:02d}/{len(agents)}] {aid}: FAIL (missing_prompt_file)", flush=True)
            continue

        prompt_path = root / "gateway-bot" / prompt_file
        if not prompt_path.exists():
            row["reason"] = f"prompt_not_found:{prompt_file}"
            results.append(row)
            print(f"[{idx:02d}/{len(agents)}] {aid}: FAIL ({row['reason']})", flush=True)
            continue

        text = prompt_path.read_text(encoding="utf-8", errors="ignore")
        low = text.lower()
        row["preview"] = text[:180].replace("\n", " ")

        if "daarwizz" in low:
            row["status"] = "PASS"
            row["reason"] = "prompt_mentions_daarwizz"
        else:
            row["status"] = "FAIL"
            row["reason"] = "prompt_missing_daarwizz_reference"

        results.append(row)
        print(f"[{idx:02d}/{len(agents)}] {aid}: {row['status']} ({row['reason']})", flush=True)

elif mode == "runtime":
    prompt = (
        "Who is DAARWIZZ in DAARION network? "
        "Answer briefly in 1 sentence and mention that DAARWIZZ is the main orchestrator/mayor."
    )
    deny_patterns = [
        r"не знаю\s+daarwizz",
        r"don't know\s+daarwizz",
        r"do not know\s+daarwizz",
        r"no information about daarwizz",
        r"не\s+знаю\s+хто\s+це",
    ]
    pos_patterns = [r"оркестратор", r"orchestrator", r"мер", r"mayor", r"entry point", r"вхідн"]

    for idx, agent in enumerate(agents, 1):
        aid = agent["id"]
        req_body = {"prompt": prompt, "max_tokens": 100, "temperature": 0.1}
        req = urlreq.Request(
            url=f"{router}/v1/agents/{aid}/infer",
            data=json.dumps(req_body).encode("utf-8"),
            headers={"Content-Type": "application/json"},
            method="POST",
        )

        row = {"agent": aid, "status": "FAIL", "reason": "", "preview": ""}
        try:
            with urlreq.urlopen(req, timeout=timeout_s) as resp:
                data = json.loads(resp.read().decode("utf-8", errors="ignore"))
                text = (data.get("response") or "").strip()
                low = text.lower()
                row["preview"] = text[:220].replace("\n", " ")

                deny = any(re.search(p, low) for p in deny_patterns)
                pos = any(re.search(p, low) for p in pos_patterns)
                if deny:
                    row["status"] = "FAIL"
                    row["reason"] = "explicit_deny"
                elif pos:
                    row["status"] = "PASS"
                    row["reason"] = "role_context_found"
                else:
                    row["status"] = "FAIL"
                    row["reason"] = "no_role_context"
        except Exception as e:
            row["status"] = "FAIL"
            row["reason"] = f"http_error:{type(e).__name__}"

        results.append(row)
        print(f"[{idx:02d}/{len(agents)}] {aid}: {row['status']} ({row['reason']})", flush=True)
        time.sleep(0.05)
else:
    raise SystemExit(f"[FAIL] unsupported mode: {mode}")

summary = {
    "mode": mode,
    "total": len(results),
    "pass": sum(1 for r in results if r["status"] == "PASS"),
    "fail": sum(1 for r in results if r["status"] == "FAIL"),
}
payload = {"summary": summary, "results": results}
out_json.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")

print(json.dumps(summary, ensure_ascii=False), flush=True)
print(f"[INFO] details: {out_json}", flush=True)

if summary["fail"] > 0:
    failed = [r["agent"] for r in results if r["status"] == "FAIL"]
    raise SystemExit(f"[FAIL] DAARWIZZ awareness failed for: {', '.join(failed)}")

print("[OK] DAARWIZZ awareness canary passed", flush=True)
PY
