#!/usr/bin/env python3
"""3단계: 실거래 (monitor_rules + 빗썸 주문). LIVE_TRADING_ENABLED=1 필수."""
import argparse
import runpy
import sys
from pathlib import Path

runpy.run_path(str(Path(__file__).resolve().parent / "_bootstrap.py"))

from deepcoin.env_loader import ENV_FILE, env_status  # noqa: E402

from config import (
    GT_INITIAL_CASH_KRW,
    LIVE_DAILY_KRW_MAX,
    LIVE_DAILY_LOSS_LIMIT_KRW,
    LIVE_MAX_TRADES_PER_DAY,
    LIVE_TRADING_ENABLED,
    MONITOR_LOOP_SLEEP_SEC,
)
from deepcoin.ops.live_trader import LiveTrader

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="WLD 실거래 (06)")
    parser.add_argument("--once", action="store_true", help="1회만 실행")
    args = parser.parse_args()
    st = env_status()
    print(
        f"[06] 운영 설정 · LIVE=ON · "
        f"초기₩{GT_INITIAL_CASH_KRW:,} · 일한도₩{LIVE_DAILY_KRW_MAX:,} · "
        f"일손실₩{LIVE_DAILY_LOSS_LIMIT_KRW:,} · max_trades={LIVE_MAX_TRADES_PER_DAY}"
    )
    print(
        f"[06] .env 로더={st.get('loader')} · 파일={ENV_FILE.name} · "
        f"LIVE_TRADING_ENABLED(raw)={st.get('live_trading_enabled')!r}"
    )
    if st.get("loader") == "fallback":
        print(
            "주의: python-dotenv 미설치 — fallback 파서 사용. "
            "권장: pip install python-dotenv (requirements.txt)"
        )
    if not LIVE_TRADING_ENABLED:
        print(
            "오류: LIVE_TRADING_ENABLED=0 — dry-run은 제거되었습니다. "
            ".env 에 LIVE_TRADING_ENABLED=1 설정 후 재실행하세요."
        )
        sys.exit(1)
    trader = LiveTrader()
    if args.once:
        trader.run_once()
    else:
        trader.run_loop(MONITOR_LOOP_SLEEP_SEC)
