"""PostHog-loop playbook, step 4: the protocol, before any data is built.

It names the label, the metrics, the two internal quality safeguards the model
must beat, the margins, the bands the PostHog property carries, and the holdout
salt: the held-out users are fixed by this salt now, before anything is scored.
Its class is `fixture`.
"""

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")

# Pre-registered with the protocol: this salt fixes who is held out.
HOLDOUT_SALT = "posthog-loop-holdout-v1"
protocol = api.post(
    "/protocols",
    {
        "name": "repeat purchase against the quality safeguards, with a holdout (fixture)",
        "provenance": "fixture",
        "labels": [state["label_id"]],
        "metrics": ["auc", "lift@10pct"],
        "arms": ["gbm_default", "gbm_tuned"],
        "margins": {"auc": 0.01, "lift@10pct": 0.0},
        "holdout_salt": HOLDOUT_SALT,
        "banding_policy": {
            "bands": [
                {"band": "high", "min_score": 0.6},
                {"band": "medium", "min_score": 0.3},
                {"band": "low", "min_score": 0.0},
            ],
            "list_size": 100,
            "alert_budget": 50,
        },
    },
    key="posthog-loop-protocol",
)
print(f"provenance: {protocol['provenance']}")
print(f"declared_at: {protocol['declared_at']}")
state.update(
    protocol_id=protocol["protocol_id"],
    banding_policy=protocol["banding_policy"],
    banding_policy_fingerprint=protocol["banding_policy_fingerprint"],
    holdout_salt=HOLDOUT_SALT,
)
