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, combine_close_observations


RAW_CSV_PATH = Path("data/weather_raw_full.csv")
OUTPUT_CSV_PATH = Path("data/weather_formatted_1s.csv")


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

    df_raw = load_raw_csv(RAW_CSV_PATH)
    print(f"Fichier brut chargé : {RAW_CSV_PATH}")
    print(f"  Lignes : {len(df_raw)}, colonnes : {list(df_raw.columns)}")
    print(f"  Type d'index : {type(df_raw.index)}")

    df_fmt = combine_close_observations(df_raw, freq="1s", agg="mean")
    print(f"Après combinaison (1s) : {len(df_fmt)} lignes")

    OUTPUT_CSV_PATH.parent.mkdir(parents=True, exist_ok=True)
    df_fmt.to_csv(OUTPUT_CSV_PATH, index_label="time")
    print(f"✔ Fichier formaté écrit dans : {OUTPUT_CSV_PATH.resolve()}")


if __name__ == "__main__":
    main()
