"""Ross1000 9-digit ISTAT country code resolution.

Ross1000 requires full 9-digit ISTAT country codes (e.g. Italy = "100000100"),
NOT the 3-digit codes used by C59.

The mapping is: ISO-2 → 9-digit ISTAT geonomenclature code.
Italy is always "100000100".
"""

from __future__ import annotations

from typing import Final

# Italy's 9-digit ISTAT code — used as the default for Italian residents
ITALY_9DIGIT_CODE: Final[str] = "100000100"

# ISO-2 → 9-digit ISTAT geonomenclature code
# These are the official Regione Liguria / ISTAT codes for Ross1000 XML.
ROSS1000_COUNTRY_CODES: Final[dict[str, str]] = {
    "AD": "200000043",  # Andorra
    "AE": "300000647",  # United Arab Emirates
    "AF": "300000660",  # Afghanistan
    "AG": "400000459",  # Antigua and Barbuda
    "AL": "200000070",  # Albania
    "AM": "300000077",  # Armenia
    "AO": "500000330",  # Angola
    "AR": "400000528",  # Argentina
    "AT": "200000038",  # Austria
    "AU": "600000800",  # Australia
    "AZ": "300000078",  # Azerbaijan
    "BA": "200000093",  # Bosnia and Herzegovina
    "BB": "400000469",  # Barbados
    "BD": "300000666",  # Bangladesh
    "BE": "200000017",  # Belgium
    "BF": "500000236",  # Burkina Faso
    "BG": "200000068",  # Bulgaria
    "BH": "300000640",  # Bahrain
    "BI": "500000328",  # Burundi
    "BJ": "500000284",  # Benin
    "BN": "300000703",  # Brunei
    "BO": "400000516",  # Bolivia
    "BR": "400000508",  # Brazil
    "BS": "400000453",  # Bahamas
    "BT": "300000675",  # Bhutan
    "BW": "500000391",  # Botswana
    "BY": "200000073",  # Belarus
    "BZ": "400000421",  # Belize
    "CA": "400000404",  # Canada
    "CD": "500000322",  # Democratic Republic of the Congo
    "CF": "500000306",  # Central African Republic
    "CG": "500000318",  # Congo
    "CH": "200000039",  # Switzerland
    "CI": "500000272",  # Cote d'Ivoire
    "CL": "400000512",  # Chile
    "CM": "500000302",  # Cameroon
    "CN": "300000720",  # China
    "CO": "400000480",  # Colombia
    "CR": "400000436",  # Costa Rica
    "CU": "400000448",  # Cuba
    "CV": "500000247",  # Cape Verde
    "CY": "300000600",  # Cyprus
    "CZ": "200000061",  # Czech Republic
    "DE": "200000004",  # Germany
    "DJ": "500000338",  # Djibouti
    "DK": "200000008",  # Denmark
    "DM": "400000460",  # Dominica
    "DO": "400000456",  # Dominican Republic
    "DZ": "500000208",  # Algeria
    "EC": "400000500",  # Ecuador
    "EE": "200000053",  # Estonia
    "EG": "500000220",  # Egypt
    "ER": "500000336",  # Eritrea
    "ES": "200000011",  # Spain
    "ET": "500000334",  # Ethiopia
    "FI": "200000032",  # Finland
    "FJ": "600000815",  # Fiji
    "FR": "200000001",  # France
    "GA": "500000314",  # Gabon
    "GB": "200000006",  # United Kingdom
    "GD": "400000473",  # Grenada
    "GE": "300000076",  # Georgia
    "GH": "500000276",  # Ghana
    "GM": "500000252",  # Gambia
    "GN": "500000260",  # Guinea
    "GQ": "500000310",  # Equatorial Guinea
    "GR": "200000009",  # Greece
    "GT": "400000416",  # Guatemala
    "GW": "500000257",  # Guinea-Bissau
    "GY": "400000488",  # Guyana
    "HN": "400000424",  # Honduras
    "HR": "200000092",  # Croatia
    "HT": "400000452",  # Haiti
    "HU": "200000064",  # Hungary
    "ID": "300000700",  # Indonesia
    "IE": "200000007",  # Ireland
    "IL": "300000624",  # Israel
    "IN": "300000664",  # India
    "IQ": "300000612",  # Iraq
    "IR": "300000616",  # Iran
    "IS": "200000024",  # Iceland
    "IT": "100000100",  # Italy
    "JM": "400000464",  # Jamaica
    "JO": "300000628",  # Jordan
    "JP": "300000732",  # Japan
    "KE": "500000346",  # Kenya
    "KG": "300000083",  # Kyrgyzstan
    "KH": "300000696",  # Cambodia
    "KM": "500000375",  # Comoros
    "KN": "400000449",  # Saint Kitts and Nevis
    "KP": "300000724",  # North Korea
    "KR": "300000728",  # South Korea
    "KW": "300000636",  # Kuwait
    "KZ": "300000079",  # Kazakhstan
    "LA": "300000684",  # Laos
    "LB": "300000604",  # Lebanon
    "LC": "400000465",  # Saint Lucia
    "LI": "200000037",  # Liechtenstein
    "LK": "300000669",  # Sri Lanka
    "LR": "500000268",  # Liberia
    "LS": "500000395",  # Lesotho
    "LT": "200000055",  # Lithuania
    "LU": "200000018",  # Luxembourg
    "LV": "200000054",  # Latvia
    "LY": "500000216",  # Libya
    "MA": "500000204",  # Morocco
    "MD": "200000074",  # Moldova
    "ME": "200000097",  # Montenegro
    "MG": "500000370",  # Madagascar
    "MK": "200000096",  # North Macedonia
    "ML": "500000232",  # Mali
    "MM": "300000676",  # Myanmar
    "MN": "300000716",  # Mongolia
    "MR": "500000228",  # Mauritania
    "MT": "200000046",  # Malta
    "MU": "500000373",  # Mauritius
    "MV": "300000667",  # Maldives
    "MW": "500000386",  # Malawi
    "MX": "400000412",  # Mexico
    "MY": "300000701",  # Malaysia
    "MZ": "500000366",  # Mozambique
    "NA": "500000389",  # Namibia
    "NE": "500000240",  # Niger
    "NG": "500000288",  # Nigeria
    "NI": "400000432",  # Nicaragua
    "NL": "200000003",  # Netherlands
    "NO": "200000028",  # Norway
    "NP": "300000672",  # Nepal
    "NZ": "600000804",  # New Zealand
    "OM": "300000649",  # Oman
    "PA": "400000442",  # Panama
    "PE": "400000504",  # Peru
    "PG": "600000801",  # Papua New Guinea
    "PH": "300000708",  # Philippines
    "PK": "300000662",  # Pakistan
    "PL": "200000060",  # Poland
    "PS": "300000625",  # Palestinian Territory
    "PT": "200000010",  # Portugal
    "PY": "400000520",  # Paraguay
    "QA": "300000644",  # Qatar
    "RO": "200000066",  # Romania
    "RS": "200000098",  # Serbia
    "RU": "200000075",  # Russia
    "RW": "500000324",  # Rwanda
    "SA": "300000632",  # Saudi Arabia
    "SB": "600000806",  # Solomon Islands
    "SC": "500000355",  # Seychelles
    "SD": "500000224",  # Sudan
    "SE": "200000030",  # Sweden
    "SG": "300000706",  # Singapore
    "SI": "200000091",  # Slovenia
    "SK": "200000063",  # Slovakia
    "SL": "500000264",  # Sierra Leone
    "SM": "200000047",  # San Marino
    "SN": "500000248",  # Senegal
    "SO": "500000342",  # Somalia
    "SR": "400000492",  # Suriname
    "ST": "500000311",  # Sao Tome and Principe
    "SV": "400000428",  # El Salvador
    "SY": "300000608",  # Syria
    "SZ": "500000393",  # Eswatini
    "TD": "500000244",  # Chad
    "TG": "500000280",  # Togo
    "TH": "300000680",  # Thailand
    "TJ": "300000082",  # Tajikistan
    "TL": "300000626",  # Timor-Leste
    "TM": "300000080",  # Turkmenistan
    "TN": "500000212",  # Tunisia
    "TO": "600000817",  # Tonga
    "TR": "200000052",  # Turkey
    "TT": "400000472",  # Trinidad and Tobago
    "TV": "600000807",  # Tuvalu
    "TW": "300000736",  # Taiwan
    "TZ": "500000352",  # Tanzania
    "UA": "200000072",  # Ukraine
    "UG": "500000350",  # Uganda
    "US": "400000400",  # United States
    "UY": "400000524",  # Uruguay
    "UZ": "300000081",  # Uzbekistan
    "VA": "200000045",  # Vatican City
    "VC": "400000467",  # Saint Vincent and the Grenadines
    "VE": "400000484",  # Venezuela
    "VN": "300000690",  # Vietnam
    "VU": "600000816",  # Vanuatu
    "WS": "600000819",  # Samoa
    "YE": "300000653",  # Yemen
    "ZA": "500000388",  # South Africa
    "ZM": "500000378",  # Zambia
    "ZW": "500000382",  # Zimbabwe
    # Fallbacks for territories not in primary list
    "XK": "200000098",  # Kosovo → Serbia
    "AX": "200000032",  # Åland Islands → Finland
    "SS": "500000224",  # South Sudan → Sudan
    "BQ": "200000003",  # Bonaire → Netherlands
    "CW": "200000003",  # Curaçao → Netherlands
    "SX": "200000003",  # Sint Maarten → Netherlands
}


def get_ross1000_country_code(iso2: str | None) -> str | None:
    """Return the 9-digit ISTAT code for a given ISO-2 country code.

    Returns None if the code is unknown — callers must raise
    Ross1000ValidationError for missing mappings.
    """
    if not iso2:
        return None
    return ROSS1000_COUNTRY_CODES.get(iso2.strip().upper())
