#!/usr/bin/env python3
"""
1분봉 → N분봉 재합성 (실매 갭보정 RAM · 백테/파람서치 공통).

키움/집계기 관례: candle_time(YYYYMMDDHHMM) = 봉 **시작** 시각.
예) 3분봉 1315 = 13:15·16·17 1분봉 합산.
"""
from __future__ import annotations

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


def floor_candle_time_to_tf(candle_time: str, tf_min: int) -> str:
    """분봉 시각을 tf_min 격자 시작 시각으로 내림."""
    raw = str(candle_time or "").strip()[:12]
    if len(raw) < 12:
        return ""
    try:
        dt0 = datetime.strptime(raw, "%Y%m%d%H%M")
    except ValueError:
        return ""
    tf = max(1, int(tf_min))
    total = dt0.hour * 60 + dt0.minute
    floored = (total // tf) * tf
    nh, nm = divmod(floored, 60)
    return f"{dt0.strftime('%Y%m%d')}{nh:02d}{nm:02d}"


def add_candle_minutes(candle_time: str, minutes: int) -> str:
    """YYYYMMDDHHMM + minutes."""
    raw = str(candle_time or "").strip()[:12]
    if len(raw) < 12:
        return ""
    try:
        dt0 = datetime.strptime(raw, "%Y%m%d%H%M")
    except ValueError:
        return ""
    from datetime import timedelta
    return (dt0 + timedelta(minutes=int(minutes))).strftime("%Y%m%d%H%M")


def minute_diff(a: str, b: str) -> Optional[int]:
    """b - a (분). 파싱 실패 시 None."""
    try:
        da = datetime.strptime(str(a)[:12], "%Y%m%d%H%M")
        db = datetime.strptime(str(b)[:12], "%Y%m%d%H%M")
    except ValueError:
        return None
    return int((db - da).total_seconds() // 60)


def rollup_1m_bars_to_tf(
    bars_1m: List[Dict[str, Any]],
    tf_min: int = 3,
) -> List[Dict[str, Any]]:
    """
    1분봉 리스트(오래된→최신) → tf_min 분봉 OHLC 재합성.

    O=구간 첫 시가, H=max, L=min, C=마지막 종가, V=합.
    반환 candle_time = 구간 시작. source='rollup_1m'.

    **완전 버킷만 반환**: 구간 안 1분봉이 ``tf_min`` 개 모두 있을 때만 포함.
    (갭보정 직후 진행 중 버킷을 불완전 volume 으로 넣으면 실매 vol 필터가
    DB·백테와 어긋남 — 2026-07-16 샘표 사례)
    """
    tf = max(1, int(tf_min))
    if tf == 1:
        out: List[Dict[str, Any]] = []
        for b in bars_1m or []:
            ct = str(b.get("candle_time") or b.get("time") or "")[:12]
            if len(ct) < 12:
                continue
            out.append({
                "candle_time": ct,
                "open": float(b.get("open") or 0),
                "high": float(b.get("high") or 0),
                "low": float(b.get("low") or 0),
                "close": float(b.get("close") or 0),
                "volume": int(float(b.get("volume") or 0)),
                "is_confirmed": 1,
                "source": str(b.get("source") or "rollup_1m"),
            })
        return out

    buckets: Dict[str, Dict[str, Any]] = {}
    child_mins: Dict[str, set] = {}
    order: List[str] = []
    for b in bars_1m or []:
        ct = str(b.get("candle_time") or b.get("time") or "")[:12]
        if len(ct) < 12:
            continue
        key = floor_candle_time_to_tf(ct, tf)
        if not key:
            continue
        o = float(b.get("open") or 0)
        h = float(b.get("high") or 0)
        lo = float(b.get("low") or 0)
        c = float(b.get("close") or 0)
        v = int(float(b.get("volume") or 0))
        if c <= 0 and o <= 0:
            continue
        child_mins.setdefault(key, set()).add(ct)
        if key not in buckets:
            buckets[key] = {
                "candle_time": key,
                "open": o if o > 0 else c,
                "high": max(h, o, c, lo),
                "low": min(x for x in (lo, o, c, h) if x > 0) if any(
                    x > 0 for x in (lo, o, c, h)
                ) else 0.0,
                "close": c if c > 0 else o,
                "volume": max(0, v),
                "is_confirmed": 1,
                "source": "rollup_1m",
            }
            order.append(key)
        else:
            agg = buckets[key]
            if h > 0:
                agg["high"] = max(float(agg["high"]), h, o, c)
            pos = [x for x in (lo, o, c) if x > 0]
            if pos:
                agg["low"] = min(float(agg["low"]) if float(agg["low"]) > 0 else pos[0], *pos)
            if c > 0:
                agg["close"] = c
            elif o > 0:
                agg["close"] = o
            agg["volume"] = int(agg["volume"]) + max(0, v)

    # 자식 1M 이 tf 개 미만이면 미완성 — 실매/백테 공통으로 제외
    return [buckets[k] for k in order if len(child_mins.get(k, ())) >= tf]


def merge_fill_holes(
    primary: List[Dict[str, Any]],
    filler: List[Dict[str, Any]],
) -> Tuple[List[Dict[str, Any]], int]:
    """
    primary(DB 등)에 없는 candle_time 만 filler(합성)로 보강.
    기존 봉은 덮어쓰지 않음. 반환 (병합 리스트, 보강 개수).
    """
    by_t: Dict[str, Dict[str, Any]] = {}
    for b in primary or []:
        ct = str(b.get("candle_time") or "")[:12]
        if len(ct) >= 12:
            by_t[ct] = dict(b)
    filled = 0
    for b in filler or []:
        ct = str(b.get("candle_time") or "")[:12]
        if len(ct) < 12:
            continue
        if ct in by_t:
            continue
        by_t[ct] = dict(b)
        filled += 1
    merged = [by_t[k] for k in sorted(by_t.keys())]
    return merged, filled
