"""Tests for bce_codes_to_label registries."""

import pytest

from bce_codes_to_label import (
    address_types,
    commercial_courts,
    countries,
    currencies,
    data_types,
    identifier_kinds,
    joint_committees,
    legal_forms,
    lookup,
    mandate_functions,
    models,
    msca,
    nace,
    postal_codes,
    rejection,
    sectors,
)


# --- Input form tests (full code, suffix, value) ---


class TestInputForms:
    def test_full_code(self):
        r = joint_committees("jcc:m100")
        assert r is not None
        assert r["_key"] == "100"

    def test_suffix_with_m(self):
        r = joint_committees("m100")
        assert r is not None
        assert r["_key"] == "100"

    def test_value_only(self):
        r = joint_committees("100")
        assert r is not None
        assert r["_key"] == "100"

    def test_case_insensitive(self):
        r = countries("be")
        assert r is not None
        assert r["_key"] == "BE"

    def test_not_found(self):
        assert joint_committees("NONEXISTENT") is None


# --- Language filter ---


class TestLangFilter:
    def test_lang_fr(self):
        r = countries("BE", lang="fr")
        assert r is not None
        assert "label_fr" in r
        assert "label_nl" not in r

    def test_lang_nl(self):
        r = countries("BE", lang="nl")
        assert r is not None
        assert r["label_nl"] == "België"

    def test_lang_de(self):
        r = joint_committees("100", lang="de")
        assert r is not None
        assert "label_de" in r


# --- Global lookup ---


class TestGlobalLookup:
    def test_unique_full_code(self):
        r = lookup("jcc:m100")
        assert r is not None
        assert r["_registry"] == "joint_committees"

    def test_ambiguous_returns_none(self):
        # "01" exists in multiple registries (sectors, nace, etc.)
        assert lookup("01") is None

    def test_not_found(self):
        assert lookup("ZZZZNONEXISTENT") is None


# --- Per-registry smoke tests ---


class TestJointCommittees:
    def test_basic(self):
        r = joint_committees("100")
        assert "label_fr" in r
        assert "label_nl" in r

    def test_sub_committee(self):
        r = joint_committees("10201")
        assert r is not None


class TestNace:
    def test_versioned_key(self):
        r = nace("2003:01")
        assert r is not None
        assert r["version"] == "2003"

    def test_full_code(self):
        r = nace("nace:2003:m01")
        assert r is not None


class TestCurrencies:
    def test_eur(self):
        r = currencies("EUR")
        assert r is not None

    def test_usd(self):
        r = currencies("USD")
        assert r is not None


class TestCountries:
    def test_belgium(self):
        r = countries("BE")
        assert r["label_en"] == "Belgium"

    def test_france(self):
        r = countries("FR")
        assert r["label_fr"] == "France"


class TestDataTypes:
    def test_am1(self):
        r = data_types("am1")
        assert r is not None
        assert "label_en" in r

    def test_full_code(self):
        r = data_types("met:am1")
        assert r is not None


class TestAddressTypes:
    def test_001(self):
        r = address_types("001")
        assert r is not None
        assert "label_fr" in r


class TestCommercialCourts:
    def test_brussels(self):
        r = commercial_courts("31")
        assert r is not None
        assert "Bruxelles" in r["label_fr"]


class TestMandateFunctions:
    def test_president(self):
        r = mandate_functions("10")
        assert r is not None
        assert "Président" in r["label_fr"]


class TestLegalForms:
    def test_001(self):
        r = legal_forms("001")
        assert r is not None
        assert "label_en" in r


class TestIdentifierKinds:
    def test_1(self):
        r = identifier_kinds("1")
        assert r is not None


class TestSectors:
    def test_01(self):
        r = sectors("01")
        assert r is not None
        assert "label_en" in r


class TestPostalCodes:
    def test_brussels(self):
        r = postal_codes("1000")
        assert r is not None
        assert r["label_fr"] == "Bruxelles"


class TestMsca:
    def test_code(self):
        r = msca("1")
        assert r is not None


class TestRejection:
    def test_pdf_11(self):
        r = rejection("PDF:11")
        assert r is not None


class TestModels:
    def test_m02_f(self):
        r = models("m02-f")
        assert r is not None


# --- Utility methods ---


class TestUtilities:
    def test_all_keys(self):
        keys = countries.all_keys()
        assert "BE" in keys
        assert len(keys) > 100

    def test_get_alias(self):
        assert countries.get("BE") == countries("BE")
