# tests/export_station_data.py
from __future__ import annotations

from pathlib import Path
import sys
from contextlib import closing


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

from meteo.config import InfluxSettings
from meteo.influx_client import create_influx_client
from meteo.station_config import default_station_config
from meteo.export import export_station_data


def main() -> None:
    """
    Exporte les données de la station météo vers un fichier CSV brut.

    Par défaut, on exporte les 7 derniers jours dans `data/weather_raw_7d.csv`.
    """
    settings = InfluxSettings.from_env()
    station_config = default_station_config()

    print("Configuration InfluxDB :")
    print(f"  URL    : {settings.url}")
    print(f"  Org    : {settings.org}")
    print(f"  Bucket : {settings.bucket}")
    print()

    with closing(create_influx_client(settings)) as client:
        print("→ Export des 7 derniers jours…")
        output_path = export_station_data(
            client=client,
            bucket=settings.bucket,
            config=station_config,
            start="-7d",                       # à ajuster plus tard si besoin
            stop=None,                         # now()
            output_path="data/weather_raw_7d.csv",
            file_format="csv",
        )

    print()
    print(f"✔ Export terminé : {output_path}")


if __name__ == "__main__":
    main()
