"""채점 보조 — 일치 수 계산, 패턴 요약, 패턴 갭."""
from typing import List, Dict, Any

LOW_HIGH_CUT = 22  # curator_helpers.py 와 동일


def score_picks_against_draw(picks: List[Dict[str, Any]],
                              win_nums: List[int],
                              bonus: int) -> Dict[str, Any]:
    """4계층 중 한 그룹(예: core_picks 5세트) vs 추첨 결과 채점.

    picks 는 [{numbers, risk_tag, reason}] 리스트.
    """
    if not picks:
        return {"avg_match": None, "best_match": 0, "five_plus_prizes": 0, "best_tier": None}

    win_set = set(win_nums)
    matches = []
    for p in picks:
        nums = p.get("numbers") or []
        m = len(set(nums) & win_set)
        matches.append((m, p.get("risk_tag")))

    avg = sum(m for m, _ in matches) / len(matches)
    best_match, best_tier = max(matches, key=lambda x: x[0])
    five_plus = sum(1 for m, _ in matches if m >= 3)  # 5등 이상

    # tier별 평균 → 가장 잘 맞은 risk_tag
    tier_scores: Dict[str, List[int]] = {}
    for m, t in matches:
        if t:
            tier_scores.setdefault(t, []).append(m)
    if tier_scores:
        best_tier = max(tier_scores.items(),
                        key=lambda kv: sum(kv[1]) / len(kv[1]))[0]

    return {
        "avg_match": round(avg, 2),
        "best_match": best_match,
        "five_plus_prizes": five_plus,
        "best_tier": best_tier,
    }


def summarize_pattern(nums: List[int]) -> Dict[str, int]:
    """한 세트의 패턴 요약 — 저/고, 홀/짝, 합계."""
    nums = sorted(nums)
    odd = sum(1 for n in nums if n % 2 == 1)
    low = sum(1 for n in nums if n <= LOW_HIGH_CUT)
    return {
        "odd_count": odd,
        "even_count": 6 - odd,
        "low_count": low,
        "high_count": 6 - low,
        "sum": sum(nums),
    }


def aggregate_pattern_summaries(summaries: List[Dict[str, int]]) -> Dict[str, float]:
    """여러 세트의 패턴 요약 → 평균(low_avg, odd_avg, sum_avg)."""
    if not summaries:
        return {"low_avg": None, "odd_avg": None, "sum_avg": None}
    n = len(summaries)
    return {
        "low_avg": round(sum(s["low_count"] for s in summaries) / n, 2),
        "odd_avg": round(sum(s["odd_count"] for s in summaries) / n, 2),
        "sum_avg": round(sum(s["sum"] for s in summaries) / n, 1),
    }


def compute_pattern_delta(user_summary: Dict[str, float],
                          draw_summary: Dict[str, float]) -> str:
    """사용자 평균 vs 추첨 패턴의 가장 큰 격차 1~2개를 한 줄로."""
    if not user_summary or user_summary.get("low_avg") is None:
        return ""
    deltas = []
    if user_summary.get("low_avg") is not None and draw_summary.get("low_avg") is not None:
        d = round(user_summary["low_avg"] - draw_summary["low_avg"], 2)
        if abs(d) >= 0.5:
            sign = "+" if d > 0 else ""
            deltas.append(("저번호", d, f"저번호 편향 {sign}{d}"))
    if user_summary.get("sum_avg") is not None and draw_summary.get("sum_avg") is not None:
        d = round(user_summary["sum_avg"] - draw_summary["sum_avg"], 1)
        if abs(d) >= 10:
            sign = "+" if d > 0 else ""
            deltas.append(("합계", d, f"합계 {sign}{d}"))
    if user_summary.get("odd_avg") is not None and draw_summary.get("odd_avg") is not None:
        d = round(user_summary["odd_avg"] - draw_summary["odd_avg"], 2)
        if abs(d) >= 0.5:
            sign = "+" if d > 0 else ""
            deltas.append(("홀짝", d, f"홀짝 {sign}{d}"))
    deltas.sort(key=lambda x: -abs(x[1]))
    return " / ".join(d[2] for d in deltas[:2])
