"""Tests du filtrage des sets par thèmes."""

from pathlib import Path

import pytest

from lib.rebrickable.filter_sets import filter_sets_by_theme, parse_theme_ids


def test_parse_theme_ids_strips_and_validates() -> None:
    """Nettoie la liste et refuse une valeur vide."""
    assert parse_theme_ids(" 274 , 602 ,620") == ["274", "602", "620"]
    with pytest.raises(ValueError):
        parse_theme_ids(" , , ")


def test_filter_sets_by_theme(tmp_path: Path) -> None:
    """Conserve uniquement les sets des thèmes ciblés avec pièces et préserve l'entête."""
    source = tmp_path / "sets.csv"
    destination = tmp_path / "filtered.csv"
    overrides = tmp_path / "overrides.csv"
    source.write_text(
        "set_num,name,year,theme_id,num_parts,img_url\n"
        "75936,T. rex Rampage,2019,602,3120,https://example\n"
        "43221,100 Years of Disney Animation Icons,2023,710,0,https://example\n"
        "75944,Indominus rex vs. Ankylosaurus,2020,602,1000,https://example\n"
        "10757,Raptor Rescue Truck,2018,620,0,https://example\n"
    )
    overrides.write_text("set_num,num_parts\n75936,3121\n")

    filter_sets_by_theme(source, destination, ["602"], overrides)

    assert destination.read_text() == (
        "set_num,name,year,theme_id,num_parts,img_url\n"
        "75936,T. rex Rampage,2019,602,3121,https://example\n"
        "75944,Indominus rex vs. Ankylosaurus,2020,602,1000,https://example\n"
    )
