import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from app.jobs.grading_helpers import (
    score_picks_against_draw,
    summarize_pattern,
    compute_pattern_delta,
)


def test_score_picks_against_draw_basic():
    win_nums = [3, 11, 17, 25, 33, 41]
    bonus = 8
    picks = [
        {"numbers": [3, 11, 17, 25, 33, 41], "risk_tag": "안정"},  # 6 일치
        {"numbers": [1, 2, 3, 4, 5, 6],      "risk_tag": "공격"},   # 1 일치
        {"numbers": [3, 11, 17, 4, 5, 6],    "risk_tag": "안정"},   # 3 일치 → 5등
    ]
    out = score_picks_against_draw(picks, win_nums, bonus)
    # 함수가 round(avg, 2) 로 반환하므로 rounded 비교
    assert out["avg_match"] == 3.33
    assert out["best_match"] == 6
    assert out["five_plus_prizes"] == 2  # 3개 이상 카운트(5등 이상)
    assert out["best_tier"] == "안정"


def test_summarize_pattern():
    nums = [3, 11, 17, 25, 33, 41]
    s = summarize_pattern(nums)
    # 저번호(<=22) 3개, 고번호 3개, 모두 홀수이므로 홀:짝 = 6:0
    assert s["low_count"] == 3
    assert s["odd_count"] == 6
    assert s["sum"] == 130


def test_compute_pattern_delta_picks_dominant_axis():
    # 사용자가 평균 저번호 4.2개 / 추첨 평균 3 → 저번호 편향 +1.2
    user = {"low_avg": 4.2, "odd_avg": 3.4, "sum_avg": 124}
    draw = {"low_avg": 3.0, "odd_avg": 3.0, "sum_avg": 142}
    delta = compute_pattern_delta(user, draw)
    assert "저번호" in delta or "low" in delta
    assert "+1.2" in delta or "1.2" in delta
