"""Deposit-flight playbook, step 3: a value-aware outcome label.

`deposit_flight_45d` is true when, within 45 days of an account's event, a
single `transfer.out` moves at least 80% of the balance out
(`balance_share >= 0.8`). The value matters: a transfer of a small share of
the balance is not flight. It is a derived pattern label, so every account
event is a supervision point and no outcome table is needed.
"""

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

label = api.post(
    "/pattern-labels",
    {
        "name": "deposit_flight_45d",
        "entity_type": "account",
        "source": {
            "kind": "derived",
            "rule": {
                "template": "custom",
                "outcome": "presence",
                "horizon_days": 45,
                "predicate": {
                    "kind": "pattern",
                    "pattern": {
                        "window_agg": {
                            "agg": "count",
                            "match": {
                                "event_type_pattern": "transfer.out",
                                "attribute_filters": [
                                    {"attr_key": "balance_share", "op": "gte", "value": 0.8}
                                ],
                            },
                            "over_days": 45,
                            "op": "gte",
                            "value": 1,
                        }
                    },
                },
            },
        },
    },
    key="deposit-flight-label",
)
print(f"label: {label['name']}, task: {label['task']}")
state.update(label_id=label["label_id"])
