# scripts/list_time_gaps.py
from __future__ import annotations

from pathlib import Path
import sys


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.gaps import find_time_gaps


CSV_PATH = Path("data/weather_minutely.csv")


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)}")

    gaps = find_time_gaps(df)
    total_missing = sum(g.missing_intervals for g in gaps)

    print()
    print("=== Gaps temporels détectés ===")
    print(f"Nombre de gaps        : {len(gaps)}")
    print(f"Total minutes manquantes (théoriques) : {total_missing}")
    print()

    if not gaps:
        print("✔ Aucun gap détecté, la série est parfaitement régulière.")
        return

    print("Top 10 des gaps les plus longs :")
    gaps_sorted = sorted(gaps, key=lambda g: g.missing_intervals, reverse=True)[:10]
    for g in gaps_sorted:
        print(
            f"- De {g.before} à {g.after} "
            f"(durée: {g.duration}, manquants: {g.missing_intervals}, "
            f"de {g.missing_start} à {g.missing_end})"
        )


if __name__ == "__main__":
    main()
