"""
REAL TRADING EXECUTOR — Only loaded when PAPER_MODE=false.

DO NOT MODIFY unless explicitly asked and paper trading thresholds are met:
  - Sharpe Ratio > 0.5 (over 4+ weeks)
  - Win Rate > 52%
  - Calibration Score > 0.7
  - Explicit `make promote` confirmation

This file is intentionally minimal until paper trading validates the strategy.
"""
import logging
import os
from typing import Optional

from bot.risk.manager import Order
from bot.executor.paper import Trade
from bot.data.db import Database

log = logging.getLogger(__name__)


class RealExecutor:
    """
    Real money executor using py-clob-client.
    Requires: POLYMARKET_API_KEY, POLYMARKET_SECRET, POLYMARKET_PASSPHRASE, WALLET_PRIVATE_KEY
    """

    def __init__(self, db: Database) -> None:
        # Safety check
        if os.getenv("PAPER_MODE", "true").lower() == "true":
            raise RuntimeError("RealExecutor instantiated while PAPER_MODE=true — this is a bug!")

        log.critical("REAL EXECUTOR ACTIVE — Real USDC will be spent")
        self._db = db
        self._client = self._init_client()

    def _init_client(self):
        try:
            from py_clob_client.client import ClobClient
            from py_clob_client.clob_types import ApiCreds

            host = "https://clob.polymarket.com"
            key = os.getenv("WALLET_PRIVATE_KEY", "")
            chain_id = 137  # Polygon

            creds = ApiCreds(
                api_key=os.getenv("POLYMARKET_API_KEY", ""),
                api_secret=os.getenv("POLYMARKET_SECRET", ""),
                api_passphrase=os.getenv("POLYMARKET_PASSPHRASE", ""),
            )
            return ClobClient(host, key=key, chain_id=chain_id, creds=creds)
        except ImportError:
            raise ImportError("py-clob-client not installed. Run: pip install py-clob-client")

    async def execute(self, order: Order) -> Optional[Trade]:
        """Execute real order on Polymarket CLOB."""
        # TODO: Implement after paper trading phase validates strategy
        # See: https://docs.polymarket.com/developers/clob/examples
        raise NotImplementedError("Real executor not yet implemented — complete paper trading phase first")

    def get_portfolio(self):
        raise NotImplementedError("Real portfolio tracking not yet implemented")
