"""Tests for taxonomy module — parsing, lookups, and loading."""

from mcgibs.taxonomy import Taxonomy, load_taxonomy


def test_taxonomy_from_dict(taxonomy_sample: Taxonomy):
    """Taxonomy parses all top-level sections from dict."""
    assert len(taxonomy_sample.disciplines) == 3
    assert len(taxonomy_sample.hazards) == 3
    assert len(taxonomy_sample.measurements) == 3


def test_taxonomy_layergroup(taxonomy_sample: Taxonomy):
    """layergroup() returns the curated scientific category for known layers."""
    assert taxonomy_sample.layergroup("AMSR2_Sea_Ice_Concentration_12km_Monthly") == "Sea Ice"
    assert taxonomy_sample.layergroup("AIRS_L3_Surface_Air_Temperature_Daily_Day") == "Temperature"
    assert (
        taxonomy_sample.layergroup("MODIS_Terra_CorrectedReflectance_TrueColor")
        == "Corrected Reflectance"
    )


def test_taxonomy_layergroup_unknown(taxonomy_sample: Taxonomy):
    """layergroup() returns None for unknown layers."""
    assert taxonomy_sample.layergroup("NONEXISTENT_LAYER") is None


def test_taxonomy_tags(taxonomy_sample: Taxonomy):
    """tags() returns search keywords for known layers."""
    tags = taxonomy_sample.tags("AMSR2_Sea_Ice_Concentration_12km_Monthly")
    assert tags is not None
    assert "ice" in tags
    assert "amsr2" in tags


def test_taxonomy_tags_unknown(taxonomy_sample: Taxonomy):
    """tags() returns None for unknown layers."""
    assert taxonomy_sample.tags("NONEXISTENT_LAYER") is None


def test_taxonomy_layers_for_discipline(taxonomy_sample: Taxonomy):
    """layers_for_discipline() returns layer IDs belonging to a discipline."""
    layers = taxonomy_sample.layers_for_discipline("Cryosphere")
    assert "AMSR2_Sea_Ice_Concentration_12km_Monthly" in layers


def test_taxonomy_layers_for_discipline_case_insensitive(taxonomy_sample: Taxonomy):
    """layers_for_discipline() is case-insensitive."""
    layers = taxonomy_sample.layers_for_discipline("cryosphere")
    assert "AMSR2_Sea_Ice_Concentration_12km_Monthly" in layers


def test_taxonomy_layers_for_discipline_unknown(taxonomy_sample: Taxonomy):
    """layers_for_discipline() returns empty list for unknown disciplines."""
    assert taxonomy_sample.layers_for_discipline("Nonexistent") == []


def test_taxonomy_layers_for_hazard(taxonomy_sample: Taxonomy):
    """layers_for_hazard() returns layer IDs belonging to a hazard category."""
    # Our sample hazards don't have matching measurements,
    # so this should return empty for the sample data
    layers = taxonomy_sample.layers_for_hazard("Fires")
    # Fires -> Active Fires -> but Active Fires isn't a measurement in our sample
    assert isinstance(layers, list)


def test_taxonomy_empty_dict():
    """Taxonomy handles empty dict gracefully."""
    tax = Taxonomy({})
    assert tax.disciplines == {}
    assert tax.hazards == {}
    assert tax.measurements == {}
    assert tax.layergroup("anything") is None
    assert tax.tags("anything") is None
    assert tax.layers_for_discipline("any") == []


def test_load_taxonomy_succeeds():
    """load_taxonomy() loads the bundled taxonomy.json file."""
    tax = load_taxonomy()
    assert tax is not None
    assert len(tax.disciplines) > 0
    assert len(tax.measurements) > 0
