"""PostHog-loop playbook, step 11: publish the comparison, baseline bar included.

The document carries the baseline evaluation (the bar, written down first)
and the model's verdict against the same protocol, each number with the
fingerprint of the report that holds it. Its class is the protocol's:
`fixture`.

The incrementality readout of step 10 is not in this document: it is a live
measurement over a window, not an archived report with a fingerprint, and on
this fixture its outcomes are generated.
"""

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("posthog-loop")

document = state.path.parent / "posthog-loop-published-results.json"
published = subprocess.run(
    [
        "tessera-doctest",
        "publish",
        "--protocol-id",
        state["protocol_id"],
        "--evaluation-id",
        state["evaluation_id"],
        "--baseline-id",
        state["baseline_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)
