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

import pytest
from analyzer import score_combination, build_analysis_cache


@pytest.fixture
def cache():
    # build_analysis_cache expects [(drw_no, [n1,n2,n3,n4,n5,n6]), ...] tuples
    fake_draws = [
        (1, [1, 2, 3, 4, 5, 6]),
        (2, [7, 8, 9, 10, 11, 12]),
    ]
    return build_analysis_cache(fake_draws)


def test_score_default_uses_fixed_weights(cache):
    """weights=None은 기존 fixed [0.25, 0.30, 0.20, 0.15, 0.10]과 동등."""
    s = score_combination([1, 2, 3, 4, 5, 6], cache)
    assert "score_total" in s
    assert 0.0 <= s["score_total"] <= 2.0
    for k in ("score_frequency", "score_fingerprint", "score_gap",
              "score_cooccur", "score_diversity"):
        assert k in s


def test_score_with_custom_weights_sums_correctly(cache):
    """weights=[1,0,0,0,0]은 score_total == score_frequency."""
    s = score_combination([1, 2, 3, 4, 5, 6], cache, weights=[1.0, 0.0, 0.0, 0.0, 0.0])
    assert s["score_total"] == pytest.approx(s["score_frequency"], rel=1e-3)


def test_score_with_uniform_weights(cache):
    """weights=[0.2]*5는 단순 평균."""
    s = score_combination([1, 2, 3, 4, 5, 6], cache, weights=[0.2] * 5)
    expected = 0.2 * (s["score_frequency"] + s["score_fingerprint"]
                      + s["score_gap"] + s["score_cooccur"] + s["score_diversity"])
    assert s["score_total"] == pytest.approx(expected, rel=1e-3)


def test_score_weights_wrong_length_raises(cache):
    with pytest.raises((ValueError, AssertionError)):
        score_combination([1, 2, 3, 4, 5, 6], cache, weights=[0.5, 0.5])
