"""Tests de la préparation de corrélation pièces/minifigs."""

from pathlib import Path

from lib.rebrickable.minifig_parts_correlation import build_correlation_rows, build_minifigs_per_year


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


def test_build_correlation_rows_merges_filtered_and_catalog(tmp_path: Path) -> None:
    """Construit les lignes de corrélation pour filtrés et catalogue global."""
    filtered_counts_path = tmp_path / "minifig_counts_by_set.csv"
    write_csv(
        filtered_counts_path,
        "set_num,set_id,name,year,minifig_count\n"
        "123-1,123,Set A,2020,2\n"
        "124-1,124,Set B,2021,1\n",
    )
    filtered_sets_path = tmp_path / "sets_enriched.csv"
    write_csv(
        filtered_sets_path,
        "set_num,num_parts\n"
        "123-1,300\n"
        "124-1,150\n",
    )
    all_sets_path = tmp_path / "sets.csv"
    write_csv(
        all_sets_path,
        "set_num,name,year,theme_id,num_parts\n"
        "123-1,Set A,2020,1,300\n"
        "124-1,Set B,2021,1,150\n"
        "200-1,Set C,2019,1,100\n",
    )
    inventories_path = tmp_path / "inventories.csv"
    write_csv(
        inventories_path,
        "id,version,set_num\n"
        "10,1,123-1\n"
        "20,2,123-1\n"
        "30,1,124-1\n"
        "40,1,200-1\n",
    )
    inventory_minifigs_path = tmp_path / "inventory_minifigs.csv"
    write_csv(
        inventory_minifigs_path,
        "inventory_id,fig_num,quantity\n"
        "10,fig-a,1\n"
        "20,fig-a,2\n"
        "30,fig-b,1\n"
        "40,fig-c,3\n",
    )

    rows = build_correlation_rows(
        filtered_counts_path,
        filtered_sets_path,
        all_sets_path,
        inventories_path,
        inventory_minifigs_path,
    )

    assert rows == [
        {"scope": "filtered", "set_num": "123-1", "num_parts": "300", "minifig_count": "2"},
        {"scope": "filtered", "set_num": "124-1", "num_parts": "150", "minifig_count": "1"},
        {"scope": "catalog", "set_num": "123-1", "num_parts": "300", "minifig_count": "2"},
        {"scope": "catalog", "set_num": "124-1", "num_parts": "150", "minifig_count": "1"},
        {"scope": "catalog", "set_num": "200-1", "num_parts": "100", "minifig_count": "3"},
    ]


def test_build_minifigs_per_year_computes_averages(tmp_path: Path) -> None:
    """Calcule les moyennes annuelles minifigs/set pour filtrés et catalogue."""
    filtered_counts_path = tmp_path / "minifig_counts_by_set.csv"
    write_csv(
        filtered_counts_path,
        "set_num,set_id,name,year,minifig_count\n"
        "123-1,123,Set A,2020,2\n"
        "124-1,124,Set B,2020,1\n"
        "125-1,125,Set C,2021,3\n"
        "126-1,126,Set D,2026,4\n",
    )
    all_sets_path = tmp_path / "sets.csv"
    write_csv(
        all_sets_path,
        "set_num,name,year,theme_id,num_parts\n"
        "123-1,Set A,2020,1,300\n"
        "124-1,Set B,2020,1,150\n"
        "125-1,Set C,2021,1,100\n"
        "200-1,Set D,2020,1,50\n"
        "126-1,Set D,2026,1,80\n",
    )
    inventories_path = tmp_path / "inventories.csv"
    write_csv(
        inventories_path,
        "id,version,set_num\n"
        "10,1,123-1\n"
        "20,1,124-1\n"
        "30,1,125-1\n"
        "40,1,200-1\n"
        "50,1,126-1\n",
    )
    inventory_minifigs_path = tmp_path / "inventory_minifigs.csv"
    write_csv(
        inventory_minifigs_path,
        "inventory_id,fig_num,quantity\n"
        "10,fig-a,2\n"
        "20,fig-b,1\n"
        "30,fig-c,3\n"
        "40,fig-d,4\n"
        "50,fig-e,5\n",
    )

    rows = build_minifigs_per_year(
        filtered_counts_path,
        all_sets_path,
        inventories_path,
        inventory_minifigs_path,
    )

    assert rows == [
        {"scope": "filtered", "year": "2020", "average_minifigs_per_set": "1.500", "set_count": "2"},
        {"scope": "filtered", "year": "2021", "average_minifigs_per_set": "3.000", "set_count": "1"},
        {"scope": "catalog", "year": "2020", "average_minifigs_per_set": "2.333", "set_count": "3"},
        {"scope": "catalog", "year": "2021", "average_minifigs_per_set": "3.000", "set_count": "1"},
    ]
