# scripts/plot_monthly_patterns.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_monthly_climatology, compute_monthly_means
from meteo.plots import plot_monthly_boxplots, plot_monthly_anomalies


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

BOXPLOT_KEYS = ["temperature", "humidity", "pressure", "wind_speed", "illuminance"]
ANOMALY_KEYS = ["temperature", "humidity", "illuminance"]


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()

    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

    box_vars = [VARIABLES_BY_KEY[key] for key in BOXPLOT_KEYS]
    boxplot_path = OUTPUT_DIR / "monthly_boxplots.png"
    plot_monthly_boxplots(df=df, variables=box_vars, output_path=boxplot_path)
    print(f"✔ Boxplots mensuels : {boxplot_path}")

    anomaly_vars = [VARIABLES_BY_KEY[key] for key in ANOMALY_KEYS]
    monthly_means = compute_monthly_means(df=df, columns=[v.column for v in anomaly_vars])
    climatology = compute_monthly_climatology(df=df, columns=[v.column for v in anomaly_vars])

    anomaly_path = OUTPUT_DIR / "monthly_anomalies.png"
    plot_monthly_anomalies(
        monthly_means=monthly_means,
        climatology=climatology,
        variables=anomaly_vars,
        output_path=anomaly_path,
    )
    print(f"✔ Anomalies mensuelles : {anomaly_path}")

    print("✔ Graphiques mensuels générés.")


if __name__ == "__main__":
    main()
