# tests/test_influx_entities.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.schema import (
    list_measurements,
    list_measurement_tag_keys,
    list_measurement_tag_values,
)


def main() -> None:
    """
    Explore les tags des measurements du bucket :

    - affiche les keys de tags pour chaque measurement
    - si un tag `entity_id` est présent, affiche la liste de ses valeurs
    """
    settings = InfluxSettings.from_env()

    print(f"Bucket InfluxDB : {settings.bucket}")
    print()

    with closing(create_influx_client(settings)) as client:
        measurements = list_measurements(client, settings.bucket)

        if not measurements:
            print("⚠ Aucun measurement trouvé dans ce bucket.")
            return

        for meas in measurements:
            print(f"Measurement « {meas} »")
            tag_keys = list_measurement_tag_keys(client, settings.bucket, meas)
            if not tag_keys:
                print("  (aucun tag trouvé)")
                print()
                continue

            print("  Tag keys :")
            for key in tag_keys:
                print(f"    - {key}")

            if "entity_id" in tag_keys:
                entity_ids = list_measurement_tag_values(
                    client,
                    settings.bucket,
                    meas,
                    tag="entity_id",
                )
                print("  entity_id possibles :")
                for eid in entity_ids:
                    print(f"    - {eid}")

            print()


if __name__ == "__main__":
    main()
