#!/usr/bin/env python3
"""REBALANCE_CADENCE_GATE_V1 — spec/formulas/domains/portfolio.yaml.

Mandatory weekly (Sat/Sun) and monthly (1/11/21) cadence checks always emit a
review, but actual rebalancing execution is allowed only when transition utility
after tax cost is positive or a hard risk block is active.
governance/todo/v8_9_p3_adoption_plan.yaml P3-D.
"""
from __future__ import annotations

import argparse
import json
from datetime import date
from pathlib import Path

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

WEEKLY_WEEKDAYS = {5, 6}  # Saturday=5, Sunday=6 (date.weekday())
MONTHLY_MID_CHECK_DAYS = {1, 11, 21}


def cadence_check_required(check_date: date, event_driven_trigger: bool = False) -> tuple[bool, str | None]:
    if check_date.weekday() in WEEKLY_WEEKDAYS:
        return True, "weekly_rebalance_required"
    if check_date.day in MONTHLY_MID_CHECK_DAYS:
        return True, "mid_check_required"
    if event_driven_trigger:
        return True, "event_driven_trigger"
    return False, None


def evaluate_rebalance_gate(
    check_date: date,
    transition_utility_after_tax_cost_krw: float | None,
    hard_risk_block_active: bool | None,
    event_driven_trigger: bool = False,
) -> dict:
    required, reason = cadence_check_required(check_date, event_driven_trigger)
    if not required:
        return {
            "cadence_check_required": False,
            "review_emitted": False,
            "rebalance_execution_allowed": False,
            "cadence_trigger_reason": None,
        }

    if transition_utility_after_tax_cost_krw is None and hard_risk_block_active is None:
        return {
            "cadence_check_required": True,
            "review_emitted": True,
            "rebalance_execution_allowed": False,
            "cadence_trigger_reason": reason,
            "gate": "DATA_MISSING",
        }

    allowed = bool(hard_risk_block_active) or (
        transition_utility_after_tax_cost_krw is not None and transition_utility_after_tax_cost_krw > 0
    )
    return {
        "cadence_check_required": True,
        "review_emitted": True,
        "rebalance_execution_allowed": allowed,
        "cadence_trigger_reason": reason,
    }


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--date", default=None, help="YYYY-MM-DD, default today")
    ap.add_argument("--transition-utility", type=float, default=None)
    ap.add_argument("--hard-risk-block", action="store_true")
    ap.add_argument("--event-driven-trigger", action="store_true")
    ap.add_argument("--out", default=str(DEFAULT_OUT))
    args = ap.parse_args()

    check_date = date.fromisoformat(args.date) if args.date else date.today()
    result = {
        "formula_id": "REBALANCE_CADENCE_GATE_V1",
        **evaluate_rebalance_gate(check_date, args.transition_utility, args.hard_risk_block, args.event_driven_trigger),
        "check_date": check_date.isoformat(),
    }
    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())
