"""Tests des visualisations temporelles des couleurs."""

import matplotlib
from pathlib import Path

from lib.plots.colors_timeline import plot_colors_heatmap, plot_translucent_share


matplotlib.use("Agg")


def test_plot_translucent_share(tmp_path: Path) -> None:
    """Produit un graphique de part de translucides et diversité des couleurs."""
    timeline_path = tmp_path / "colors_timeline.csv"
    milestones_path = tmp_path / "milestones.csv"
    destination = tmp_path / "figures" / "step15" / "colors_translucent_share.png"
    timeline_path.write_text(
        "year,colors_distinct,colors_new,colors_lost,share_translucent,total_quantity,top_colors\n"
        "2020,2,2,0,0.25,100,Blue (60),Red (40)\n"
        "2021,3,1,0,0.40,120,Blue (50),Trans-Black (48)\n"
    )
    milestones_path.write_text("year,description\n2020,Jalon Test\n")

    plot_translucent_share(timeline_path, milestones_path, destination)

    assert destination.exists()
    assert destination.stat().st_size > 0


def test_plot_colors_heatmap(tmp_path: Path) -> None:
    """Génère une heatmap année × couleur."""
    matrix_path = tmp_path / "colors_year_color_matrix.csv"
    destination_linear = tmp_path / "figures" / "step15" / "colors_heatmap_linear.png"
    destination_log = tmp_path / "figures" / "step15" / "colors_heatmap_log.png"
    destination_share = tmp_path / "figures" / "step15" / "colors_heatmap_share.png"
    matrix_path.write_text(
        "year,color_rgb,is_translucent,color_name,quantity_total\n"
        "2020,AAAAAA,false,Gray,5\n"
        "2020,BBBBBB,true,Trans-Black,2\n"
        "2021,AAAAAA,false,Gray,3\n"
        "2021,CCCCCC,false,Blue,4\n"
    )

    plot_colors_heatmap(matrix_path, destination_linear, use_log_scale=False)
    plot_colors_heatmap(matrix_path, destination_log, use_log_scale=True)
    plot_colors_heatmap(matrix_path, destination_share, normalize_by_year=True)

    assert destination_linear.exists()
    assert destination_linear.stat().st_size > 0
    assert destination_log.exists()
    assert destination_log.stat().st_size > 0
    assert destination_share.exists()
    assert destination_share.stat().st_size > 0
