"""Security-lake playbook, step 11: the detection-quality report, as a published result.

The evaluation judges the model against the protocol of step 6 only. The
report is written in the published-results format: its class is the
protocol's (`fixture`), every number cites the fingerprint of the report that
holds it, and its appendix lists the calls that reproduce it (step 12).
"""

import os
import subprocess
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))  # playbooks/playbook_api.py
from playbook_api import Api, State

api, state = Api(), State("security-lake")

evaluation = api.post(
    "/evaluations",
    {"model_id": state["model_id"], "protocol_id": state["protocol_id"], "seed": 0},
    key="security-lake-evaluation",
)
detail = api.wait(
    f"/evaluations/{evaluation['report_id']}",
    lambda item: (item.get("evaluation") or {}).get("status") in ("succeeded", "failed"),
    what="the protocol evaluation",
)
print(f"evaluation: {detail['evaluation']['status']}")
verdict = (detail.get("report") or {}).get("verdict") or {}
print(f"verdict provenance: {verdict.get('provenance')}")
document = state.path.parent / "security-lake-published-results.json"
published = subprocess.run(
    [
        "tessera-doctest",
        "publish",
        "--protocol-id",
        state["protocol_id"],
        "--evaluation-id",
        evaluation["report_id"],
        "--out",
        str(document),
        "--repo-root",
        os.environ["TESSERA_REPO_ROOT"],
    ],
    capture_output=True,
    text=True,
)
print(published.stdout)
if published.returncode != 0:
    sys.exit(published.stderr)
state.update(published_results=str(document))
