#!/usr/bin/env python3
"""
스캘핑·모멘텀 백테스트 공통 로더 — backtest_web / param_search 가
동일한 캔들·유니버스·손익 계산을 쓰도록 단일 진입점.
"""
from __future__ import annotations

from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple

from kis_trader.backtest.backtest_portfolio_common import (
    attach_scalp_trade_pnl,
    backtest_slip_pct,
    build_budget_warning,
    fee_and_slot_from_env_row,
    merge_portfolio_into_params,
    min_invest_ratio_of_slot,
    resolve_portfolio_params,
    summarize_trades,
)
from kis_trader.engine import scalping_engine as se

SCALP_STRATEGY_ID = "SCALP"
MOMENTUM_STRATEGY_ID = "MOMENTUM"


def date_keys(start: str, end: str) -> Tuple[str, str, str, str]:
    """YYYY-MM-DD → candle_time 키 및 ymd."""
    start_key = start.replace("-", "") + "0000"
    end_key = end.replace("-", "") + "2359"
    return start_key, end_key, start_key[:8], end_key[:8]


def resolve_scalp_universe(
    start_ymd: str,
    end_ymd: str,
    *,
    use_saved_history: bool,
    strategy_id: str = SCALP_STRATEGY_ID,
) -> Tuple[Optional[Dict[str, List[str]]], str, int, int]:
    """
    backtest_web 유니버스 해석과 동일.

    Returns:
        (universe_by_slot, source_label, history_slot_count, scan_interval_min)
    """
    if use_saved_history and strategy_id:
        try:
            from kis_trader.database.db_manager import get_db as _get_ext_db

            history = _get_ext_db().get_universe_by_candle_time(
                strategy_id=strategy_id,
                start_ymd=start_ymd,
                end_ymd=end_ymd,
            )
            if history:
                return history, "history", len(history), 1
        except Exception:
            pass
    return None, "all", 0, 1


def load_scalp_candles_by_code(
    db,
    start_key: str,
    end_key: str,
    rsi_period: int = 3,
) -> Tuple[Dict[str, List[Dict]], int]:
    """ws_candles 1분봉 전 종목 로드."""
    codes_raw = db.conn.execute(
        "SELECT DISTINCT code FROM ws_candles WHERE timeframe=1 "
        "AND candle_time >= %s AND candle_time <= %s ORDER BY code",
        [start_key, end_key],
    ).fetchall()
    codes = [r["code"] for r in codes_raw]

    candles_by_code: Dict[str, List[Dict]] = {}
    total_candles = 0
    min_bars = int(rsi_period) + 5

    for code in codes:
        rows = db.conn.execute(
            "SELECT candle_time, open, high, low, close, volume "
            "FROM ws_candles WHERE timeframe=1 AND code=%s "
            "AND candle_time >= %s AND candle_time <= %s AND is_confirmed=1 "
            "ORDER BY candle_time ASC",
            [code, start_key, end_key],
        ).fetchall()
        if len(rows) < min_bars:
            continue
        candles_by_code[code] = [dict(r) for r in rows]
        total_candles += len(rows)

    return candles_by_code, total_candles


def run_scalping_backtest_web_aligned(
    candles_by_code: Dict[str, List[Dict]],
    params: Dict[str, Any],
    universe_by_slot: Optional[Dict[str, List[str]]],
    *,
    slot_money: float,
    fee_rate: float,
    sell_tax: float,
    max_stocks: Optional[int] = None,
    total_budget_krw: Optional[float] = None,
    meta_out: Optional[Dict[str, Any]] = None,
    mode: str = "reversal",
) -> List[Dict]:
    """엔진 1회 + 웹과 동일 손익 부착 (reversal / momentum)."""
    engine_params = dict(params)
    engine_params["slot_money"] = float(slot_money)
    if max_stocks is not None:
        engine_params["max_stocks"] = int(max_stocks)
    if total_budget_krw is not None:
        tb = float(total_budget_krw)
        engine_params["total_budget_krw"] = tb if tb > 0 else float(
            int(engine_params.get("max_stocks") or 3) * slot_money
        )
    if universe_by_slot is not None:
        engine_params.setdefault("scan_interval_min", 1)
    engine_params.setdefault("portfolio_mode", True)

    if str(mode).strip().lower() == "momentum":
        from kis_trader.backtest import momentum_backtest_common as mbc
        trades = mbc.run_momentum_backtest_web_aligned(
            candles_by_code, engine_params, universe_by_slot,
            slot_money=float(slot_money),
            fee_rate=float(fee_rate),
            sell_tax=float(sell_tax),
            max_stocks=int(engine_params.get("max_stocks") or max_stocks or 3),
            total_budget_krw=float(engine_params.get("total_budget_krw") or 0),
            meta_out=meta_out,
        )
    else:
        trades = se.run_scalping_backtest(
            candles_by_code, engine_params, universe_by_slot=universe_by_slot,
        )

    attach_scalp_trade_pnl(
        trades, fee_rate=fee_rate, sell_tax=sell_tax,
        slip_pct=backtest_slip_pct(engine_params),
    )
    if meta_out is not None and str(mode).strip().lower() != "momentum":
        skip_stats = engine_params.get("_portfolio_skip_stats") or {}
        meta_out["skip_stats"] = dict(skip_stats)
        meta_out["engine_params"] = engine_params
    return trades


def resolve_scalp_portfolio_params(
    env_row: Optional[Dict[str, Any]],
    base_defaults: Optional[Dict[str, Any]] = None,
    *,
    strategy: str = "SCALP",
    slot_money: Optional[float] = None,
    max_stocks: Optional[int] = None,
    total_budget_krw: Optional[float] = None,
) -> Dict[str, Any]:
    """웹·파라서치 공통 포트폴리오 해석."""
    return resolve_portfolio_params(
        env_row,
        base_defaults,
        strategy=strategy,
        slot_money=slot_money,
        max_stocks=max_stocks,
        total_budget_krw=total_budget_krw,
    )


def merge_scalp_portfolio_into_params(
    params: Dict[str, Any],
    portfolio: Dict[str, Any],
) -> Dict[str, Any]:
    return merge_portfolio_into_params(params, portfolio)


def build_scalp_budget_warning(
    portfolio: Dict[str, Any],
    skip_stats: Optional[Dict[str, Any]] = None,
    *,
    strategy: str = "SCALP",
) -> Optional[str]:
    ratio = min_invest_ratio_of_slot({}, strategy=strategy)
    return build_budget_warning(portfolio, skip_stats, min_invest_ratio=ratio)


def summarize_scalp_trades(
    trades: List[Dict],
    *,
    total_budget_krw: float,
    period_days: int = 1,
) -> Dict[str, Any]:
    return summarize_trades(
        trades,
        total_budget_krw=total_budget_krw,
        period_days=period_days,
    )


def fee_and_slot_from_env(
    row: Optional[Dict[str, Any]],
    *,
    strategy: str = "SCALP",
) -> Tuple[float, float, float]:
    return fee_and_slot_from_env_row(row, strategy=strategy)
