"""
test_engine.py
Unit test untuk Rule Engine, Scoring, Priority Engine, dan Simulator.
Jalankan dengan: pytest
"""
from rule_engine import run_rules, CATEGORIES
from scoring import compute_growth_score
from priority_engine import prioritize
from simulator import simulate


def make_weak_data(**overrides):
    base = {
        "title": "",
        "title_length": 0,
        "meta_description": "",
        "meta_description_length": 0,
        "has_viewport": False,
        "h1_count": 0,
        "h1_tags": [],
        "h2_count": 0,
        "cta_count": 0,
        "cta_examples": [],
        "form_count": 0,
        "has_faq": False,
        "has_testimonial": False,
        "emails": [],
        "whatsapp_links": [],
        "phones": [],
        "has_address_hint": False,
        "social_links": [],
        "images_total": 4,
        "images_missing_alt": 4,
        "nav_links_count": 5,
        "word_count": 50,
        "load_time_seconds": 5.0,
        "is_https": False,
    }
    base.update(overrides)
    return base


def make_strong_data(**overrides):
    base = {
        "title": "A Reasonably Descriptive and Ideal Website Title",
        "title_length": 45,
        "meta_description": "Deskripsi meta yang jelas dan menarik untuk pengguna serta mesin pencari.",
        "meta_description_length": 75,
        "has_viewport": True,
        "h1_count": 1,
        "h1_tags": ["Judul Utama"],
        "h2_count": 3,
        "cta_count": 3,
        "cta_examples": ["Beli Sekarang", "Hubungi Kami"],
        "form_count": 1,
        "has_faq": True,
        "has_testimonial": True,
        "emails": ["hello@bisnis.com"],
        "whatsapp_links": ["https://wa.me/6281234567890"],
        "phones": ["081234567890"],
        "has_address_hint": True,
        "social_links": ["https://instagram.com/bisnis"],
        "images_total": 5,
        "images_missing_alt": 0,
        "nav_links_count": 6,
        "word_count": 900,
        "load_time_seconds": 1.2,
        "is_https": True,
    }
    base.update(overrides)
    return base


def test_weak_website_gets_low_score_and_many_recommendations():
    scores, recs, issues_count = run_rules(make_weak_data())
    result = compute_growth_score(scores)
    assert result["growth_score"] < 50
    assert issues_count > 5
    assert all(c in scores for c in CATEGORIES)


def test_strong_website_gets_high_score():
    scores, recs, issues_count = run_rules(make_strong_data())
    result = compute_growth_score(scores)
    assert result["growth_score"] >= 85
    assert issues_count <= 2


def test_prioritize_ranks_quick_wins_first():
    scores, recs, _ = run_rules(make_weak_data())
    ranked = prioritize(recs)
    assert ranked[0]["priority_rank"] == 1
    # Rekomendasi impact tinggi + effort rendah harus ada di peringkat atas
    high_impact_low_effort = [r for r in ranked if r["impact"] == "high" and r["effort"] == "low"]
    if high_impact_low_effort:
        assert ranked.index(high_impact_low_effort[0]) < len(ranked) / 2


def test_simulator_increases_score_after_applying_recommendations():
    scores, recs, _ = run_rules(make_weak_data())
    ranked = prioritize(recs)
    selected_ids = [r["id"] for r in ranked[:3]]
    sim_result = simulate(scores, ranked, selected_ids)
    assert sim_result["after_score"] >= sim_result["before_score"]
    assert sim_result["estimated_gain"] >= 0


def test_compute_growth_score_weights_sum_to_full_range():
    perfect_scores = {c: 100 for c in CATEGORIES}
    result = compute_growth_score(perfect_scores)
    assert result["growth_score"] == 100
    assert result["grade"] == "Excellent"

    zero_scores = {c: 0 for c in CATEGORIES}
    result_zero = compute_growth_score(zero_scores)
    assert result_zero["growth_score"] == 0
    assert result_zero["grade"] == "Needs Serious Attention"
