"""Tests for collections constants and routing sets."""

from foehn.collections import (
    COLLECTIONS,
    FORECAST_CSV_COLLECTIONS,
    GRIB2_COLLECTIONS,
    NETCDF_COLLECTIONS,
    discover,
)


def test_collections_keys_are_strings():
    for key, collection_id in COLLECTIONS.items():
        assert isinstance(key, str) and key
        assert isinstance(collection_id, str) and collection_id.startswith("ch.")


def test_routing_sets_are_subsets_of_collections():
    keys = set(COLLECTIONS.keys())
    assert keys >= FORECAST_CSV_COLLECTIONS
    assert keys >= GRIB2_COLLECTIONS
    assert keys >= NETCDF_COLLECTIONS


def test_routing_sets_are_mutually_exclusive():
    """A collection should belong to at most one routing set."""
    all_sets = [FORECAST_CSV_COLLECTIONS, GRIB2_COLLECTIONS, NETCDF_COLLECTIONS]
    for i, a in enumerate(all_sets):
        for b in all_sets[i + 1 :]:
            assert a.isdisjoint(b), f"Overlap between routing sets: {a & b}"


def test_collection_ids_are_unique():
    ids = list(COLLECTIONS.values())
    assert len(ids) == len(set(ids)), "Duplicate STAC collection IDs found"


def test_discover_returns_all_collections():
    rows = discover()
    assert len(rows) == len(COLLECTIONS)


def test_discover_dict_keys():
    for row in discover():
        assert set(row.keys()) == {"category", "key", "collection_id"}
