# scripts/plot_correlations.py
"""Produit les nuages de points ciblés entre variables sélectionnées."""

from __future__ import annotations

import argparse
from pathlib import Path
import sys
from typing import Sequence

import pandas as pd


PROJECT_ROOT = Path(__file__).resolve().parents[3]
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

from meteo.dataset import load_raw_csv
from meteo.variables import Variable, VARIABLES, VARIABLES_BY_KEY, iter_variable_pairs
from meteo.plots import plot_scatter_pair


CSV_PATH = Path("data/weather_minutely.csv")
DOC_DIR = Path(__file__).resolve().parent.parent
SCATTER_DIR = DOC_DIR / "figures" / "pairwise_scatter"


def _select_variables(keys: Sequence[str] | None) -> list[Variable]:
    if not keys:
        return list(VARIABLES)
    try:
        selected = [VARIABLES_BY_KEY[key] for key in keys]
    except KeyError as exc:
        raise SystemExit(f"Variable inconnue : {exc.args[0]!r}.") from exc
    return selected


def _generate_pairwise_scatter(
    df: pd.DataFrame,
    variables: Sequence[Variable],
    *,
    sample_step: int,
) -> None:
    pairs = iter_variable_pairs()
    selected = [(vx, vy) for vx, vy in pairs if vx in variables and vy in variables]
    if not selected:
        print("⚠ Aucun couple sélectionné pour les nuages de points.")
        return

    SCATTER_DIR.mkdir(parents=True, exist_ok=True)
    for var_x, var_y in selected:
        output_path = SCATTER_DIR / f"scatter_{var_x.key}_vs_{var_y.key}.png"
        print(f"→ Scatter {var_y.key} vs {var_x.key}")
        plot_scatter_pair(df, var_x=var_x, var_y=var_y, output_path=output_path, sample_step=sample_step)
    print(f"✔ {len(selected)} nuage(s) de points généré(s) dans {SCATTER_DIR}.")


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description="Génère des nuages de points pairwise entre variables.")
    parser.add_argument(
        "--dataset",
        type=Path,
        default=CSV_PATH,
        help="Dataset à utiliser (par défaut : data/weather_minutely.csv).",
    )
    parser.add_argument(
        "--variables",
        nargs="*",
        help="Restreint l'analyse à certaines clés de variables.",
    )
    parser.add_argument(
        "--scatter-step",
        type=int,
        default=20,
        help="Pas d'échantillonnage pour les nuages de points individuels.",
    )

    args = parser.parse_args(argv)

    dataset_path = args.dataset
    if not dataset_path.exists():
        raise SystemExit(f"Dataset introuvable : {dataset_path}")

    df = load_raw_csv(dataset_path)
    print(f"Dataset chargé : {dataset_path} ({len(df)} lignes)")
    print()

    variables = _select_variables(args.variables)

    _generate_pairwise_scatter(df, variables, sample_step=args.scatter_step)
    print("✔ Terminé.")
    return 0


if __name__ == "__main__":  # pragma: no cover
    raise SystemExit(main())
