#!/usr/bin/env python3
"""TREND_FILTER_GATE_V1 — spec/formulas/domains/entry.yaml.

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

import argparse
import json
from pathlib import Path

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


def evaluate(close_price: float | None, ma120: float | None, ma120_prev: float | None) -> bool | None:
    if close_price is None or ma120 is None or ma120_prev is None:
        return None
    return close_price > ma120 and ma120 > ma120_prev


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--close", type=float, default=None)
    ap.add_argument("--ma120", type=float, default=None)
    ap.add_argument("--ma120-prev", type=float, default=None)
    ap.add_argument("--out", default=str(DEFAULT_OUT))
    args = ap.parse_args()

    result = {
        "formula_id": "TREND_FILTER_GATE_V1",
        "trend_filter_pass": evaluate(args.close, args.ma120, args.ma120_prev),
    }
    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())
