"""Deposit-flight playbook, step 12: publish the comparison and the lead time.

The evaluation judges the served model against the protocol; the document
carries that verdict and the backtest's lead-time numbers, each with the
fingerprint of the report that holds it. Class `fixture`.
"""

import json
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("deposit-flight")

evaluation = api.post(
    "/evaluations",
    {"model_id": state["serving_model"], "protocol_id": state["protocol_id"], "seed": 0},
    key="deposit-flight-evaluation",
)
judged = api.wait(
    f"/evaluations/{evaluation['report_id']}",
    lambda item: (item.get("evaluation") or {}).get("status") in ("succeeded", "failed"),
    what="the protocol evaluation",
)
status = (judged.get("evaluation") or {}).get("status")
print(f"evaluation: {status}")
if status != "succeeded":
    sys.exit(json.dumps(judged)[:3000])
document = state.path.parent / "deposit-flight-published-results.json"
published = subprocess.run(
    [
        "tessera-doctest",
        "publish",
        "--protocol-id",
        state["protocol_id"],
        "--evaluation-id",
        evaluation["report_id"],
        "--backtest-id",
        state["backtest_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)
