#!/usr/bin/env python3
"""빗썸 실계좌 잔고 조회 (운영 점검용)."""

from __future__ import annotations

import runpy
import sys
from pathlib import Path

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

from config import GT_INITIAL_CASH_KRW, LIVE_TRADING_ENABLED, SYMBOL  # noqa: E402
from deepcoin.ops.monitor import Monitor  # noqa: E402


def main() -> int:
    """
    KRW·거래 심볼 잔고를 출력합니다.

    Returns:
        0 성공, 1 API 오류.
    """
    print(f"LIVE_TRADING_ENABLED={int(LIVE_TRADING_ENABLED)}")
    print(f"GT_INITIAL_CASH_KRW=₩{GT_INITIAL_CASH_KRW:,}")
    m = Monitor(cooldown_file=None)
    raw = m.getBalances()
    if isinstance(raw, dict) and raw.get("error"):
        print("API_ERROR", raw.get("error"))
        return 1
    if not isinstance(raw, list):
        print("UNEXPECTED_RESPONSE", type(raw))
        return 1
    krw = next((x for x in raw if x.get("currency") == "KRW"), None)
    coin = next((x for x in raw if x.get("currency") == SYMBOL), None)
    if krw:
        bal = float(krw["balance"])
        print(f"KRW available=₩{bal:,.0f}")
        if bal < float(GT_INITIAL_CASH_KRW):
            print(f"  [WARN] 가용 KRW < 초기자금 ₩{GT_INITIAL_CASH_KRW:,}")
    if coin:
        print(f"{SYMBOL} qty={float(coin['balance']):.6f}")
    else:
        print(f"{SYMBOL} qty=0")
    return 0


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