# scripts/plot_seasonal_overview.py
from __future__ import annotations

from pathlib import Path

from meteo.dataset import load_raw_csv
from meteo.variables import VARIABLES_BY_KEY
from meteo.analysis import compute_rainfall_by_season
from meteo.plots import plot_seasonal_boxplots, plot_rainfall_by_season
from meteo.season import sort_season_labels, SEASON_LABELS


CSV_PATH = Path("data/weather_minutely.csv")
OUTPUT_DIR = Path("figures/seasonal")

BOXPLOT_VARIABLES = ["temperature", "humidity", "pressure", "wind_speed", "illuminance"]


def infer_season_order(df) -> list[str]:
    seasons = df["season"].dropna().unique()
    order = sort_season_labels(seasons, order=SEASON_LABELS)
    if not order:
        order = list(SEASON_LABELS)
    return order


def main() -> None:
    if not CSV_PATH.exists():
        print(f"⚠ Fichier introuvable : {CSV_PATH}")
        return

    df = load_raw_csv(CSV_PATH)
    print(f"Dataset minuté chargé : {CSV_PATH}")
    print(f"  Lignes   : {len(df)}")
    print(f"  Colonnes : {list(df.columns)}")
    print()

    if "season" not in df.columns:
        print('⚠ La colonne "season" est absente. Relancez python "docs/02 - Préparation des données/scripts/make_minutely_dataset.py".')
        return

    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
    season_order = infer_season_order(df)
    print(f"Saisons détectées : {season_order}")

    variables = [VARIABLES_BY_KEY[key] for key in BOXPLOT_VARIABLES]
    boxplot_path = OUTPUT_DIR / "seasonal_boxplots.png"
    plot_seasonal_boxplots(
        df=df,
        variables=variables,
        output_path=boxplot_path,
        season_order=season_order,
        title="Distribution des mesures par saison",
    )
    print(f"✔ Boxplots saisonniers : {boxplot_path}")

    rainfall = compute_rainfall_by_season(df=df, rate_column="rain_rate", season_column="season")
    rainfall_path = OUTPUT_DIR / "rainfall_by_season.png"
    plot_rainfall_by_season(rainfall_df=rainfall, output_path=rainfall_path)
    print(f"✔ Pluie saisonnière : {rainfall_path}")

    print("✔ Tous les graphiques saisonniers ont été générés.")


if __name__ == "__main__":
    main()
