"""Tests de construction du fichier parts_filtered.csv."""

import csv
from pathlib import Path

from lib.rebrickable.parts_inventory import write_parts_filtered


def write_csv(path: Path, headers: list[str], rows: list[list[str]]) -> None:
    """Écrit un CSV simple pour les besoins de tests."""
    with path.open("w", newline="") as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(headers)
        writer.writerows(rows)


def test_write_parts_filtered(tmp_path: Path) -> None:
    """Assemble les pièces par set avec la dernière version d'inventaire."""
    sets_path = tmp_path / "sets_enriched.csv"
    inventories_path = tmp_path / "inventories.csv"
    inventory_parts_path = tmp_path / "inventory_parts.csv"
    colors_path = tmp_path / "colors.csv"
    inventory_minifigs_path = tmp_path / "inventory_minifigs.csv"
    minifigs_path = tmp_path / "minifigs.csv"
    destination_path = tmp_path / "parts_filtered.csv"

    write_csv(
        sets_path,
        ["set_num", "set_id", "name", "num_parts", "year"],
        [
            ["1234-1", "1234", "Sample Set A", "9", "2020"],
            ["5678-1", "5678", "Sample Set B", "2", "2021"],
        ],
    )
    write_csv(
        inventories_path,
        ["id", "version", "set_num"],
        [
            ["1", "1", "1234-1"],
            ["2", "2", "1234-1"],
            ["3", "1", "5678-1"],
            ["4", "1", "fig-123"],
        ],
    )
    write_csv(
        inventory_parts_path,
        ["inventory_id", "part_num", "color_id", "quantity", "is_spare", "img_url"],
        [
            ["2", "3001", "1", "4", "False", ""],
            ["2", "3002", "2", "1", "True", ""],
            ["3", "3003", "3", "2", "False", ""],
            ["4", "mf-1", "2", "1", "False", ""],
            ["4", "mf-2", "3", "2", "False", ""],
        ],
    )
    write_csv(
        inventory_minifigs_path,
        ["inventory_id", "fig_num", "quantity"],
        [
            ["2", "fig-123", "1"],
        ],
    )
    write_csv(
        minifigs_path,
        ["fig_num", "name", "num_parts", "img_url"],
        [
            ["fig-123", "Sample Minifig", "2", ""],
        ],
    )
    write_csv(
        colors_path,
        ["id", "name", "rgb", "is_trans", "num_parts", "num_sets", "y1", "y2"],
        [
            ["1", "White", "FFFFFF", "False", "0", "0", "0", "0"],
            ["2", "Black", "000000", "True", "0", "0", "0", "0"],
            ["3", "Red", "FF0000", "False", "0", "0", "0", "0"],
        ],
    )

    write_parts_filtered(
        sets_path,
        inventories_path,
        inventory_parts_path,
        colors_path,
        inventory_minifigs_path,
        minifigs_path,
        destination_path,
    )

    with destination_path.open() as result_file:
        reader = csv.DictReader(result_file)
        rows = list(reader)

    assert rows == [
        {
            "part_num": "3001",
            "color_rgb": "FFFFFF",
            "is_translucent": "false",
            "set_num": "1234-1",
            "set_id": "1234",
            "year": "2020",
            "quantity_in_set": "4",
            "is_spare": "false",
            "is_minifig_part": "false",
        },
        {
            "part_num": "3002",
            "color_rgb": "000000",
            "is_translucent": "true",
            "set_num": "1234-1",
            "set_id": "1234",
            "year": "2020",
            "quantity_in_set": "1",
            "is_spare": "true",
            "is_minifig_part": "false",
        },
        {
            "part_num": "mf-1",
            "color_rgb": "000000",
            "is_translucent": "true",
            "set_num": "1234-1",
            "set_id": "1234",
            "year": "2020",
            "quantity_in_set": "1",
            "is_spare": "false",
            "is_minifig_part": "true",
        },
        {
            "part_num": "mf-2",
            "color_rgb": "FF0000",
            "is_translucent": "false",
            "set_num": "1234-1",
            "set_id": "1234",
            "year": "2020",
            "quantity_in_set": "2",
            "is_spare": "false",
            "is_minifig_part": "true",
        },
        {
            "part_num": "3003",
            "color_rgb": "FF0000",
            "is_translucent": "false",
            "set_num": "5678-1",
            "set_id": "5678",
            "year": "2021",
            "quantity_in_set": "2",
            "is_spare": "false",
            "is_minifig_part": "false",
        },
    ]
