"""Tests d'agrégation des couleurs par set."""

import csv
from pathlib import Path

from lib.rebrickable.colors_by_set import (
    aggregate_colors_by_set,
    build_colors_lookup,
    load_parts,
    write_colors_by_set,
)


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


def test_aggregate_colors_by_set(tmp_path: Path) -> None:
    """Regroupe les quantités par set et par couleur."""
    parts_path = tmp_path / "parts_filtered.csv"
    colors_path = tmp_path / "colors.csv"
    destination_path = tmp_path / "colors_by_set.csv"
    write_csv(
        parts_path,
        [
            "part_num",
            "color_rgb",
            "is_translucent",
            "set_num",
            "set_id",
            "year",
            "quantity_in_set",
            "is_spare",
            "is_minifig_part",
        ],
        [
            ["3001", "FFFFFF", "false", "1000-1", "1000", "2020", "2", "false", "false"],
            ["3002", "FFFFFF", "false", "1000-1", "1000", "2020", "1", "true", "false"],
            ["3003", "000000", "true", "1000-1", "1000", "2020", "4", "false", "true"],
            ["4001", "FFFFFF", "false", "2000-1", "2000", "2021", "3", "false", "true"],
            ["4002", "0033B2", "false", "2000-1", "2000", "2021", "5", "false", "false"],
        ],
    )
    write_csv(
        colors_path,
        ["id", "name", "rgb", "is_trans", "num_parts", "num_sets", "y1", "y2"],
        [
            ["1", "White", "FFFFFF", "False", "0", "0", "0", "0"],
            ["2", "Trans-Black", "000000", "True", "0", "0", "0", "0"],
            ["3", "Ignored Blue", "0033B2", "False", "0", "0", "0", "0"],
        ],
    )

    parts = load_parts(parts_path)
    colors_lookup = build_colors_lookup(colors_path)
    aggregated = aggregate_colors_by_set(parts, colors_lookup)
    write_colors_by_set(destination_path, aggregated)

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

    assert rows == [
        {
            "set_num": "1000-1",
            "set_id": "1000",
            "year": "2020",
            "color_rgb": "000000",
            "is_translucent": "true",
            "color_name": "Trans-Black",
            "quantity_total": "4",
            "quantity_non_spare": "4",
            "quantity_minifig": "4",
            "quantity_non_minifig": "0",
        },
        {
            "set_num": "1000-1",
            "set_id": "1000",
            "year": "2020",
            "color_rgb": "FFFFFF",
            "is_translucent": "false",
            "color_name": "White",
            "quantity_total": "3",
            "quantity_non_spare": "2",
            "quantity_minifig": "0",
            "quantity_non_minifig": "3",
        },
        {
            "set_num": "2000-1",
            "set_id": "2000",
            "year": "2021",
            "color_rgb": "FFFFFF",
            "is_translucent": "false",
            "color_name": "White",
            "quantity_total": "3",
            "quantity_non_spare": "3",
            "quantity_minifig": "3",
            "quantity_non_minifig": "0",
        },
    ]
