"""Diagramme de corrélation entre pièces et minifigs par set."""

from pathlib import Path
from typing import Iterable, Tuple

import matplotlib.pyplot as plt

from lib.filesystem import ensure_parent_dir
from lib.rebrickable.stats import read_rows


def load_points(path: Path, scope: str) -> Tuple[list[int], list[int]]:
    """Charge les points (x=num_parts, y=minifig_count) pour un scope donné."""
    rows = read_rows(path)
    xs: list[int] = []
    ys: list[int] = []
    for row in rows:
        if row["scope"] != scope:
            continue
        xs.append(int(row["num_parts"]))
        ys.append(int(row["minifig_count"]))
    return xs, ys


def compute_regression(points: Iterable[Tuple[int, int]]) -> Tuple[float, float]:
    """Calcule une régression linéaire simple (pente, ordonnée à l'origine)."""
    xs = [x for x, _ in points]
    ys = [y for _, y in points]
    n = len(xs)
    mean_x = sum(xs) / n
    mean_y = sum(ys) / n
    numerator = 0.0
    denominator = 0.0
    for x, y in points:
        dx = x - mean_x
        dy = y - mean_y
        numerator += dx * dy
        denominator += dx * dx
    slope = numerator / denominator if denominator != 0 else 0.0
    intercept = mean_y - slope * mean_x
    return slope, intercept


def plot_minifig_parts_correlation(correlation_path: Path, destination_path: Path) -> None:
    """Trace la corrélation pièces/minifigs pour les sets filtrés vs catalogue global."""
    filtered_x, filtered_y = load_points(correlation_path, "filtered")
    catalog_x, catalog_y = load_points(correlation_path, "catalog")
    filtered_points = list(zip(filtered_x, filtered_y))
    catalog_points = list(zip(catalog_x, catalog_y))
    if not filtered_points or not catalog_points:
        return
    filtered_slope, filtered_intercept = compute_regression(filtered_points)
    catalog_slope, catalog_intercept = compute_regression(catalog_points)
    x_min = min(min(filtered_x), min(catalog_x))
    x_max = max(max(filtered_x), max(catalog_x))

    fig, ax = plt.subplots(figsize=(10, 7))
    ax.scatter(catalog_x, catalog_y, color="#bbbbbb", alpha=0.25, s=18, label="Catalogue global")
    ax.scatter(filtered_x, filtered_y, color="#1f77b4", alpha=0.8, s=28, label="Thèmes filtrés")
    ax.plot(
        [x_min, x_max],
        [catalog_slope * x_min + catalog_intercept, catalog_slope * x_max + catalog_intercept],
        color="#555555",
        linestyle="--",
        linewidth=1.4,
        label=f"Tendance globale (pente {catalog_slope:.3f})",
    )
    ax.plot(
        [x_min, x_max],
        [filtered_slope * x_min + filtered_intercept, filtered_slope * x_max + filtered_intercept],
        color="#1f77b4",
        linestyle="-",
        linewidth=1.6,
        label=f"Tendance thèmes filtrés (pente {filtered_slope:.3f})",
    )
    ax.set_xlabel("Nombre de pièces du set")
    ax.set_ylabel("Nombre de minifigs")
    ax.set_title("Corrélation pièces / minifigs")
    ax.grid(True, linestyle="--", alpha=0.3)
    ax.legend(loc="upper left")

    ensure_parent_dir(destination_path)
    fig.tight_layout()
    fig.savefig(destination_path, dpi=160)
    plt.close(fig)
