#!/usr/bin/env python3
"""BREAKOUT_FAILURE_STOP_V1 — spec/formulas/domains/exit.yaml.

governance/todo/technical_signals_p4_adoption_plan.yaml P4-6.
"""
from __future__ import annotations

import argparse
import json
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
DEFAULT_OUT = ROOT / "Temp" / "breakout_failure_stop_v1.json"


def evaluate(prior_high: float | None, close_price: float | None, days_since_breakout: int | None) -> dict:
    if prior_high is None or close_price is None or days_since_breakout is None:
        return {"breakout_failure": None}
    breakout_failure = days_since_breakout <= 7 and close_price < prior_high
    result = {"breakout_failure": breakout_failure}
    if breakout_failure:
        result["gate"] = "SELL_RISK_EXIT_REVIEW"
        result["reason_code"] = "breakout_failure_stop"
    return result


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--prior-high", type=float, default=None)
    ap.add_argument("--close", type=float, default=None)
    ap.add_argument("--days-since-breakout", type=int, default=None)
    ap.add_argument("--out", default=str(DEFAULT_OUT))
    args = ap.parse_args()

    result = {
        "formula_id": "BREAKOUT_FAILURE_STOP_V1",
        **evaluate(args.prior_high, args.close, args.days_since_breakout),
    }
    out = Path(args.out)
    out.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
    print(json.dumps(result, ensure_ascii=False, indent=2))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
