#!/usr/bin/env python3
"""WEEKLY_LEGACY_TRANSFER_PLAN_V1 — spec/formulas/domains/cash.yaml.

A weekly legacy-stock-to-CMA transfer plan is a planning input, not deployable
cash, until the deposit is actually confirmed. governance/todo/v8_9_p3_adoption_plan.yaml P3-E.
"""
from __future__ import annotations

import argparse
import json
from pathlib import Path

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


def evaluate_transfer_plan(
    weekly_legacy_to_cma_transfer_plan_krw: float,
    transfer_confirmed: bool | None,
    transfer_confirmed_amount_krw: float | None,
) -> dict:
    confirmed = bool(transfer_confirmed)
    if not confirmed:
        return {
            "deployable_cash_contribution_krw": 0.0,
            "plan_status": "PLANNED_NOT_DEPLOYABLE",
            "planned_amount_krw": weekly_legacy_to_cma_transfer_plan_krw,
        }
    amount = transfer_confirmed_amount_krw if transfer_confirmed_amount_krw is not None else 0.0
    return {
        "deployable_cash_contribution_krw": amount,
        "plan_status": "CONFIRMED_DEPLOYABLE",
        "planned_amount_krw": weekly_legacy_to_cma_transfer_plan_krw,
    }


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--planned-amount", type=float, default=4000000.0)
    ap.add_argument("--transfer-confirmed", action="store_true")
    ap.add_argument("--confirmed-amount", type=float, default=None)
    ap.add_argument("--out", default=str(DEFAULT_OUT))
    args = ap.parse_args()

    result = {
        "formula_id": "WEEKLY_LEGACY_TRANSFER_PLAN_V1",
        **evaluate_transfer_plan(args.planned_amount, args.transfer_confirmed, args.confirmed_amount),
    }
    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())
