"""Tests du comptage de minifigs par set."""

from pathlib import Path

from lib.rebrickable.minifig_counts import build_minifig_counts_by_set


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


def test_build_minifig_counts_by_set_counts_heads_without_spares(tmp_path: Path) -> None:
    """Compte les têtes hors rechanges pour chaque set et conserve les sets à 0."""
    sets_path = tmp_path / "sets_enriched.csv"
    write_csv(
        sets_path,
        "set_num,name,year,set_id\n"
        "123-1,Set A,2020,123\n"
        "124-1,Set B,2021,124\n"
        "125-1,Set C,2022,125\n",
    )
    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-a,ffffff,false,123-1,123,2020,2,true,true\n"
        "head-b,ffffff,false,124-1,124,2021,3,false,true\n"
        "other,000000,false,124-1,124,2021,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,Head A,59\n"
        "head-b,Head B,59\n"
        "other,Other,1\n",
    )

    counts = build_minifig_counts_by_set(sets_path, parts_filtered_path, parts_catalog_path)

    assert counts == [
        {"set_num": "124-1", "set_id": "124", "name": "Set B", "year": "2021", "minifig_count": 3},
        {"set_num": "123-1", "set_id": "123", "name": "Set A", "year": "2020", "minifig_count": 1},
        {"set_num": "125-1", "set_id": "125", "name": "Set C", "year": "2022", "minifig_count": 0},
    ]
