#!/usr/bin/env python3
"""
Live trading platform runner (modular strategies + IB portfolio + risk gates).

**Paper default:** TWS / IB Gateway port **7497**, ``client_id=2`` (VRP standalone uses 1).

Schedule example (cron, America/New_York ~3:45 PM):

    cd /Users/robzingale/trading_bot && .venv/bin/python RenTech/live/run_live_platform.py

Kill switch: create ``RenTech/live/config/KILL_SWITCH`` with content ``HALT``.

DISCLAIMER: Prototype / educational. Validate in paper. Not financial advice.
"""

from __future__ import annotations

import argparse
import sys
from pathlib import Path

_REPO = Path(__file__).resolve().parents[2]
if str(_REPO) not in sys.path:
    sys.path.insert(0, str(_REPO))

from RenTech.live.orchestrator import run_from_config_path
from RenTech.live.platform_config import DEFAULT_PLATFORM_CONFIG


def main() -> None:
    ap = argparse.ArgumentParser(description="RenTech modular live platform (IBKR).")
    ap.add_argument(
        "--config",
        type=Path,
        default=DEFAULT_PLATFORM_CONFIG,
        help=f"Platform JSON (default: {DEFAULT_PLATFORM_CONFIG})",
    )
    ap.add_argument(
        "--recommend-only",
        action="store_true",
        help="Override config: compute tickets only, no orders.",
    )
    ap.add_argument(
        "--live",
        action="store_true",
        help="Override config risk.recommend_only=false (places orders).",
    )
    ap.add_argument(
        "--force-entry-now",
        action="store_true",
        help="Bypass per-strategy entry-after ET gate.",
    )
    ap.add_argument(
        "--entry-after-et",
        type=str,
        default=None,
        help="Default entry gate HH:MM ET for strategies without their own.",
    )
    ap.add_argument(
        "--audit",
        action="store_true",
        help="Run strategy audit hooks only (e.g. state vs IB positions).",
    )
    ap.add_argument(
        "--strategy",
        type=str,
        default=None,
        help="Run only this strategy id from config.",
    )
    args = ap.parse_args()

    rec_only = True if args.recommend_only else (False if args.live else None)

    run_from_config_path(
        args.config.expanduser().resolve(),
        recommend_only=rec_only,
        force_entry_now=bool(args.force_entry_now),
        entry_after_et=args.entry_after_et,
        audit_only=bool(args.audit),
        strategy_filter=args.strategy,
    )


if __name__ == "__main__":
    main()
