import pandas as pd

from keepedu_performance.core.evaluation_rules import (
    is_grade_entry_series_2026,
    requires_configured_bulletin_2026,
)
from keepedu_performance.processing.bronze.keepedu import alunos, notas
from keepedu_performance.processing.silver.keepedu import medias


ITA_SCHOOL_SERIES = [
    "1ª Série ITA",
    "2ª Série ITA",
    "3ª Série ITA",
    "9º ANO ITA",
]


def test_only_ita_students_with_validated_bulletins_are_visible():
    source = pd.DataFrame(
        {
            "SERIE": [*ITA_SCHOOL_SERIES, "ITA/IME", "1ª Série ITA (Processo Seletivo)"],
            "CODTURMA": ["EM1ITA-T", "EM2ITA-T", "EM3ITA-M", "EF9ITA", "ITA/IME-AM", "ITA_PS"],
        }
    )

    result = alunos.remove_series_indesejadas(source)
    result = alunos.remove_series_bloqueadas(result)

    assert result["SERIE"].tolist() == [
        "1ª Série ITA",
        "2ª Série ITA",
        "9º ANO ITA",
    ]


def test_ita_school_averages_are_available_for_ranking():
    source = pd.DataFrame({"SERIE": [*ITA_SCHOOL_SERIES, "ITA/IME", "PRÉ"]})

    result = medias.remove_blocked_series(source)

    assert result["SERIE"].tolist() == [
        "1ª Série ITA",
        "2ª Série ITA",
        "9º ANO ITA",
    ]


def test_only_ita_ime_and_third_grade_may_lack_a_bulletin():
    configured = ["1ª Série ITA", "2ª Série ITA", "9º ANO ITA"]
    without_bulletin = ["3ª Série ITA", "ITA/IME"]

    assert all(map(requires_configured_bulletin_2026, configured))
    assert not any(map(requires_configured_bulletin_2026, without_bulletin))


def test_ita_grades_remain_outside_grade_entry_until_rules_are_validated():
    ita = [*ITA_SCHOOL_SERIES, "ITA/IME"]

    assert set(notas.normalize_text(pd.Series(ita))).issubset(notas.SERIES_BLOQUEADAS)
    assert not any(map(is_grade_entry_series_2026, ita))
