"""
총자산 대비 GT 모델 매수율(비중) · 보유 현금 한도 · leg tier 배분.
"""

from __future__ import annotations

import json
from datetime import datetime
from pathlib import Path
from typing import Any

from config import (
    GT_BUY_PCT_LARGE_LEG,
    GT_BUY_PCT_SMALL_LEG,
    GT_INITIAL_CASH_KRW,
    GT_LARGE_LEG_TOP_PCT,
    GT_MIN_ORDER_KRW,
    MATCH_GT_TOLERANCE_MIN,
    TRADING_FEE_RATE,
)
from deepcoin.matching.load_rules import load_matched_rules
from deepcoin.paths import MATCHING_FIRE_OUTCOMES, MATCHING_MATCHED_RULES

_GT_ALLOC_ANALYSIS_CACHE: dict[str, Any] | None = None


def portfolio_totals(
    cash: float,
    qty: float,
    price: float,
) -> tuple[float, float, float]:
    """
    총보유자산·코인평가·가용현금(=총자산-평가액)을 계산합니다.

    Args:
        cash: 현금.
        qty: 보유 수량.
        price: 평가·체결가.

    Returns:
        (total_asset_krw, holding_value_krw, cash_krw).
    """
    holding = qty * price
    total = cash + holding
    return total, holding, cash


def optimal_weight_share(weight: float, weight_sum_remaining: float) -> float:
    """
    leg 내 남은 매수 비중 대비 이번 체결 최적 매수율(0~1).

    Args:
        weight: 이번 타점 weight.
        weight_sum_remaining: 동일 leg 남은 매수 weight 합.

    Returns:
        비중 비율.
    """
    if weight_sum_remaining > 0:
        return weight / weight_sum_remaining
    return 1.0


def compute_buy_amount_krw(
    cash: float,
    qty: float,
    price: float,
    weight: float,
    weight_sum_remaining: float,
    *,
    asset_pct_scale: float,
    min_order_krw: float = GT_MIN_ORDER_KRW,
    fee_rate: float = TRADING_FEE_RATE,
    ignore_weight_split: bool = False,
) -> float:
    """
    목표=총보유자산×(최적 매수율×scale), 체결=min(목표, 보유현금/(1+fee)) 로 매수 원화를 산출합니다.

    보유 현금 = 총보유자산 − 코인평가액(cash 인자).

    Args:
        cash: 보유 현금(가용 원화).
        qty: 보유 수량.
        price: 체결가.
        weight: 타점 비중.
        weight_sum_remaining: leg 내 남은 매수 weight 합.
        asset_pct_scale: leg·규칙 티어(대형/소형) 스케일.
        min_order_krw: 최소 주문 원화.
        fee_rate: 수수료율.
        ignore_weight_split: True면 weight 분할 없이 scale만 적용 (conviction 매수).

    Returns:
        매수 원화(0이면 미체결).
    """
    if price <= 0:
        return 0.0
    total_asset, _, available_cash = portfolio_totals(cash, qty, price)
    budget = max(available_cash / (1.0 + fee_rate), 0.0)
    if ignore_weight_split:
        opt_rate = asset_pct_scale
    else:
        opt_rate = optimal_weight_share(weight, weight_sum_remaining) * asset_pct_scale
    target = total_asset * opt_rate
    amount = min(target, budget)
    if budget >= min_order_krw and 0 < amount < min_order_krw:
        amount = min(min_order_krw, budget)
    return round(max(amount, 0.0), 0)


def large_leg_ids_from_past_returns(
    leg_returns: dict[int, float],
    top_pct: float = GT_LARGE_LEG_TOP_PCT,
) -> set[int]:
    """
    이미 청산된 leg의 realized return 상위 n% (인과적 tier).

    Args:
        leg_returns: leg_id → realized return %.
        top_pct: 상위 비율.

    Returns:
        large leg id set.
    """
    if not leg_returns:
        return set()
    ranked = sorted(leg_returns.items(), key=lambda x: x[1], reverse=True)
    n = max(1, int(len(ranked) * top_pct + 0.999999))
    return {lid for lid, _ in ranked[:n]}


def top_leg_ids_by_forward_return(
    trades: list[dict[str, Any]],
    top_pct: float = GT_LARGE_LEG_TOP_PCT,
) -> set[int]:
    """
    leg별 최대 forward_return 기준 상위 n% leg_id 집합.

    Args:
        trades: GT trade dict.
        top_pct: 상위 비율(0~1).

    Returns:
        대형 매수 leg_id set.
    """
    leg_ret: dict[int, float] = {}
    for t in trades:
        if t.get("action") != "sell":
            continue
        lid = int(t.get("leg_id", 0))
        ret = float(t.get("forward_return_pct") or 0.0)
        leg_ret[lid] = max(leg_ret.get(lid, 0.0), ret)
    if not leg_ret:
        return set()
    ranked = sorted(leg_ret.items(), key=lambda x: x[1], reverse=True)
    n = max(1, int(len(ranked) * top_pct + 0.999999))
    return {lid for lid, _ in ranked[:n]}


def leg_asset_pct_scale(leg_id: int, large_legs: set[int]) -> float:
    """
    leg 티어에 따른 총자산 대비 매수 스케일.

    Args:
        leg_id: leg 번호.
        large_legs: 상위 leg 집합.

    Returns:
        GT_BUY_PCT_LARGE_LEG 또는 GT_BUY_PCT_SMALL_LEG.
    """
    if leg_id in large_legs:
        return float(GT_BUY_PCT_LARGE_LEG)
    return float(GT_BUY_PCT_SMALL_LEG)


def _parse_dt(dt: str) -> datetime:
    return datetime.fromisoformat(str(dt).replace("Z", "+00:00")[:19])


def nearest_gt_leg_id(
    dt: str,
    gt_trades: list[dict[str, Any]],
    tolerance_min: int = MATCH_GT_TOLERANCE_MIN,
) -> int | None:
    """
    시각에 가장 가까운 GT trade의 leg_id (매수 우선).

    Args:
        dt: 발화 시각.
        gt_trades: GT trades.
        tolerance_min: 허용 분.

    Returns:
        leg_id 또는 None.
    """
    if not gt_trades:
        return None
    t0 = _parse_dt(dt)
    best_buy: int | None = None
    best_buy_min = float(tolerance_min) + 1.0
    best_any: int | None = None
    best_any_min = float(tolerance_min) + 1.0
    for t in gt_trades:
        try:
            t1 = _parse_dt(t["dt"])
        except ValueError:
            continue
        delta = abs((t0 - t1).total_seconds()) / 60.0
        if delta > tolerance_min:
            continue
        lid = int(t.get("leg_id", 0))
        if t.get("action") == "buy" and delta < best_buy_min:
            best_buy_min = delta
            best_buy = lid
        if delta < best_any_min:
            best_any_min = delta
            best_any = lid
    return best_buy if best_buy is not None else best_any


def load_gt_allocation_analysis(
    gt_trades: list[dict[str, Any]] | None = None,
) -> dict[str, Any]:
    """
    GT amount_krw 분석 캐시 (tier 권장 pct).

    Args:
        gt_trades: GT trades. None이면 파일 로드.

    Returns:
        analyze_gt_buy_allocation 결과.
    """
    global _GT_ALLOC_ANALYSIS_CACHE
    if _GT_ALLOC_ANALYSIS_CACHE is not None:
        return _GT_ALLOC_ANALYSIS_CACHE
    from deepcoin.ground_truth.gt_allocation_analysis import analyze_gt_buy_allocation
    from deepcoin.paths import resolve_ground_truth_file

    trades = gt_trades
    if trades is None:
        p = resolve_ground_truth_file()
        if p.is_file():
            trades = json.loads(p.read_text(encoding="utf-8")).get("trades") or []
    if not trades:
        _GT_ALLOC_ANALYSIS_CACHE = {}
        return _GT_ALLOC_ANALYSIS_CACHE
    chron = sorted(trades, key=lambda x: x["dt"])
    if not any(float(t.get("amount_krw") or 0) > 0 for t in chron):
        from deepcoin.ground_truth.ground_truth import allocate_gt_order_amounts

        allocate_gt_order_amounts(chron)
    _GT_ALLOC_ANALYSIS_CACHE = analyze_gt_buy_allocation(chron)
    return _GT_ALLOC_ANALYSIS_CACHE


def gt_tier_scale_for_trade(
    trade: dict[str, Any],
    gt_trades: list[dict[str, Any]],
    large_legs: set[int],
    *,
    analysis: dict[str, Any] | None = None,
) -> float:
    """
    GT leg tier 배분 스케일 (분석 권장값 또는 config).

    Args:
        trade: {dt, leg_id?, action, ...}.
        gt_trades: GT trades (leg 매칭).
        large_legs: 상위 leg.
        analysis: analyze_gt_buy_allocation 결과.

    Returns:
        pct_large 또는 pct_small.
    """
    from deepcoin.ground_truth.gt_allocation_analysis import gt_tier_scale_from_analysis

    lid = trade.get("leg_id")
    if lid is None:
        lid = nearest_gt_leg_id(str(trade["dt"]), gt_trades)
    if lid is None:
        return float(GT_BUY_PCT_SMALL_LEG)
    return gt_tier_scale_from_analysis(int(lid), large_legs, analysis)


def enrich_sim_trades_with_gt_weights(
    trades: list[dict[str, Any]],
    gt_trades: list[dict[str, Any]],
    *,
    causal_legs: bool = False,
) -> list[dict[str, Any]]:
    """
    규칙 발화에 GT leg_id·매수/매도 weight를 부여합니다.

    causal_legs=True: GT leg 매칭 없이 매수~매도 구간 순번 leg_id (인과적).

    Args:
        trades: {dt, action/side, price, rule_id}.
        gt_trades: GT trades (leg 매칭, causal_legs=False 일 때).
        causal_legs: 순차 leg_id.

    Returns:
        leg_id·weight가 채워진 trade dict.
    """
    from deepcoin.ground_truth.gt_model import leg_entry_weights, leg_exit_weights

    rows = sorted(trades, key=lambda x: x["dt"])
    pos = 0
    seq_leg = 0
    while pos < len(rows):
        action = rows[pos].get("action", rows[pos].get("side", ""))
        if action != "buy":
            if causal_legs:
                rows[pos]["leg_id"] = seq_leg
            elif "leg_id" not in rows[pos]:
                rows[pos]["leg_id"] = nearest_gt_leg_id(rows[pos]["dt"], gt_trades) or 0
            rows[pos]["weight"] = float(rows[pos].get("weight", 1.0))
            pos += 1
            continue
        buy_end = pos
        while buy_end < len(rows):
            a = rows[buy_end].get("action", rows[buy_end].get("side", ""))
            if a != "buy":
                break
            buy_end += 1
        buy_slice = rows[pos:buy_end]
        sell_slice: list[dict[str, Any]] = []
        sell_end = buy_end
        while sell_end < len(rows):
            a = rows[sell_end].get("action", rows[sell_end].get("side", ""))
            if a == "buy":
                break
            if a == "sell":
                sell_slice.append(rows[sell_end])
            sell_end += 1

        if causal_legs:
            leg_id = seq_leg
        else:
            leg_id = nearest_gt_leg_id(buy_slice[0]["dt"], gt_trades) or 0
        prices = [float(t["price"]) for t in buy_slice]
        buy_weights = leg_entry_weights(prices)
        for t, w in zip(buy_slice, buy_weights):
            t["leg_id"] = leg_id
            t["weight"] = round(w, 4)
            if "action" not in t and "side" in t:
                t["action"] = t["side"]

        if sell_slice:
            sw = leg_exit_weights(len(sell_slice))
            for t, w in zip(sell_slice, sw):
                t["leg_id"] = leg_id
                t["weight"] = round(w, 4)
                if "action" not in t and "side" in t:
                    t["action"] = t["side"]
        if causal_legs and sell_slice:
            seq_leg += 1
        pos = sell_end if sell_slice else buy_end
    return rows


def attach_gt_model_amounts(
    trades: list[dict[str, Any]],
    *,
    gt_trades: list[dict[str, Any]] | None = None,
    approved_rules: set[str] | None = None,
    large_legs: set[int] | None = None,
    initial_cash: float = GT_INITIAL_CASH_KRW,
    fee_rate: float = TRADING_FEE_RATE,
) -> list[dict[str, Any]]:
    """
    GT 모델 비중 + 공통 배분 엔진으로 amount_krw를 채웁니다.

    시뮬·매칭 전용: leg·tier 모두 인과적(과거 청산 leg 수익만). GT 정답 배분은
    ground_truth.allocate_gt_order_amounts 를 사용하세요.

    Args:
        trades: enrich_sim_trades_with_gt_weights 출력 또는 raw fires.
        gt_trades: GT trades. None이면 파일 로드.
        approved_rules: EV/WF 통과 rule (live scale용).
        large_legs: 상위 leg.
        initial_cash: 초기 현금.
        fee_rate: 수수료율.

    Returns:
        amount_krw·weight·leg_id가 채워진 trade dict.
    """
    from deepcoin.ground_truth.gt_allocation import allocate_order_amounts_chronological

    if gt_trades is None:
        gt_trades, _, _ = load_sizing_context_from_gt()

    enriched = enrich_sim_trades_with_gt_weights(
        list(trades),
        gt_trades,
        causal_legs=True,
    )

    allocate_order_amounts_chronological(
        enriched,
        initial_cash=initial_cash,
        fee_rate=fee_rate,
        large_legs=None,
        asset_pct_scale_fn=None,
        causal_tier=True,
    )
    return enriched


def attach_dynamic_buy_amounts(
    trades: list[dict[str, Any]],
    *,
    gt_trades: list[dict[str, Any]] | None = None,
    approved_rules: set[str] | None = None,
    large_legs: set[int] | None = None,
    initial_cash: float = GT_INITIAL_CASH_KRW,
    default_weight: float = 1.0,
    fee_rate: float = TRADING_FEE_RATE,
) -> list[dict[str, Any]]:
    """
    시뮬 발화 trade dict에 amount_krw(GT 모델·보유 현금 한도)를 채웁니다.

    attach_gt_model_amounts 별칭.
    """
    return attach_gt_model_amounts(
        trades,
        gt_trades=gt_trades,
        approved_rules=approved_rules,
        large_legs=large_legs,
        initial_cash=initial_cash,
        fee_rate=fee_rate,
    )


def load_sizing_context_from_gt(
    gt_path: Path | None = None,
) -> tuple[list[dict[str, Any]], set[int], set[str]]:
    """
    GT JSON에서 trades, 상위 leg, EV/WF 통과 rule을 로드합니다.

    Args:
        gt_path: ground_truth_trades.json.

    Returns:
        (gt_trades, large_legs, approved_rules).
    """
    from deepcoin.paths import resolve_ground_truth_file

    p = gt_path or resolve_ground_truth_file()
    trades: list[dict[str, Any]] = []
    if p.is_file():
        data = json.loads(p.read_text(encoding="utf-8"))
        trades = data.get("trades") or []
    large = top_leg_ids_by_forward_return(trades)
    return trades, large, set()
