"""Tests de l'extraction des têtes de minifigs par set."""

from pathlib import Path

from lib.rebrickable.minifigs_by_set import build_minifigs_by_set


def write_csv(path: Path, content: str) -> None:
    """Écrit un CSV brut."""
    path.write_text(content)


def test_build_minifigs_by_set_filters_spares_and_deduplicates(tmp_path) -> None:
    """Identifie les têtes de minifigs par set en dédupliquant et en excluant les rechanges."""
    parts_filtered_path = tmp_path / "parts_filtered.csv"
    write_csv(
        parts_filtered_path,
        "part_num,color_rgb,is_translucent,set_num,set_id,year,quantity_in_set,is_spare,is_minifig_part\n"
        "head-a,ffffff,false,123-1,123,2020,1,false,true\n"
        "head-b,ffffff,false,123-1,123,2020,2,false,true\n"
        "head-b,ffffff,false,123-1,123,2020,1,true,true\n"
        "head-b,ffffff,false,124-1,124,2021,1,false,true\n"
        "head-c,ffffff,false,123-1,123,2020,1,false,true\n"
        "other,000000,false,123-1,123,2020,1,false,false\n",
    )
    parts_catalog_path = tmp_path / "parts.csv"
    write_csv(
        parts_catalog_path,
        "part_num,name,part_cat_id\n"
        "head-a,Minifig Head Owen Grady,59\n"
        "head-b,\"Minifig Head, Eyebrows\",59\n"
        "head-c,Minifig Head Unknown,59\n"
        "other,Other,1\n",
    )
    inventories_path = tmp_path / "inventories.csv"
    write_csv(
        inventories_path,
        "id,version,set_num\n"
        "1,1,123-1\n"
        "2,1,124-1\n"
        "10,1,fig-owen\n"
        "11,1,fig-guard\n",
    )
    inventory_parts_path = tmp_path / "inventory_parts.csv"
    write_csv(
        inventory_parts_path,
        "inventory_id,part_num,color_id,quantity,is_spare\n"
        "10,head-a,1,1,false\n"
        "10,other,1,1,false\n"
        "11,head-b,1,1,false\n",
    )
    inventory_minifigs_path = tmp_path / "inventory_minifigs.csv"
    write_csv(
        inventory_minifigs_path,
        "inventory_id,fig_num,quantity\n"
        "1,fig-owen,1\n"
        "1,fig-guard,1\n"
        "2,fig-owen,1\n"
        "2,fig-guard,1\n",
    )
    minifigs_path = tmp_path / "minifigs.csv"
    write_csv(
        minifigs_path,
        "fig_num,name\n"
        "fig-owen,Owen Grady with Backpack\n"
        "fig-guard,Guard in Helmet with Trans-Brown Visor, Female\n",
    )
    aliases_path = tmp_path / "known_character_aliases.csv"
    write_csv(
        aliases_path,
        "alias,canonical\n"
        "Guard in Helmet with Trans-Brown Visor,Figurant\n",
    )
    genders_path = tmp_path / "known_character_genders.csv"
    write_csv(
        genders_path,
        "known_character,gender\n"
        "Owen Grady,male\n"
        "Figurant,unknown\n",
    )
    destination_path = tmp_path / "minifigs_by_set.csv"

    build_minifigs_by_set(
        parts_filtered_path,
        parts_catalog_path,
        inventories_path,
        inventory_parts_path,
        inventory_minifigs_path,
        minifigs_path,
        aliases_path,
        genders_path,
        destination_path,
    )

    assert destination_path.read_text() == (
        "set_num,part_num,known_character,fig_num,gender\n"
        "123-1,head-a,Owen Grady,fig-owen,male\n"
        "123-1,head-b,Figurant,fig-guard,unknown\n"
        "124-1,head-b,Figurant,fig-guard,unknown\n"
    )
