from __future__ import annotations

import argparse
import json
from pathlib import Path


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--hist", required=True)
    ap.add_argument("--out", required=True)
    args = ap.parse_args()
    hist = json.loads(Path(args.hist).read_text(encoding="utf-8"))
    records = hist.get("records", []) if isinstance(hist, dict) else hist
    live = [r for r in records if isinstance(r, dict) and str(r.get("source_type") or "live").lower() == "live"]
    replay = [r for r in records if isinstance(r, dict) and str(r.get("source_type") or "").lower() == "replay"]
    payload = {
        "formula_id": "LIVE_REPLAY_SEPARATION_V2",
        "replay_used_as_live_count": 0,
        "live_t20_count": len(live),
        "replay_t20_count": len(replay),
        "performance_ready": False,
    }
    Path(args.out).write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
    print(json.dumps(payload, ensure_ascii=True, indent=2))
    return 0


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