from __future__ import annotations

# One semantic prefix may have several grammatical source keys in the Moria
# matrices (for example krwawy / krwawe).  Filters operate on the semantic
# lemma, while item names are inflected for the concrete base item.
PREFIX_LEMMA_OVERRIDES = {
    "cwiekowana": "cwiekowany",
    "krotkie": "krotki",
    "pikowane": "pikowany",
    "lekkie": "lekki",
    "wzmocnione": "wzmocniony",
    "aksamitne": "aksamitny",
    "cwiekowane": "cwiekowany",
    "kuloodporne": "kuloodporny",
    "gietkie": "gietki",
    "kolcze": "kolczy",
    "szamanskie": "szamanski",
    "krwawe": "krwawy",
    "elfie": "elfi",
    "tygrysie": "tygrysi",
    "pancerne": "pancerny",
    "runiczne": "runiczny",
    "kompozytowe": "kompozytowy",
    "smiercionosne": "smiercionosny",
}

PREFIX_LEMMA_DISPLAY = {
    "cwiekowany": "ćwiekowany",
    "krotki": "krótki",
    "pikowany": "pikowany",
    "lekki": "lekki",
    "wzmocniony": "wzmocniony",
    "aksamitny": "aksamitny",
    "kuloodporny": "kuloodporny",
    "gietki": "giętki",
    "kolczy": "kolczy",
    "szamanski": "szamański",
    "krwawy": "krwawy",
    "elfi": "elfi",
    "tygrysi": "tygrysi",
    "pancerny": "pancerny",
    "runiczny": "runiczny",
    "kompozytowy": "kompozytowy",
    "smiercionosny": "śmiercionośny",
}

# m = masculine singular, f = feminine singular,
# n = neuter/invariable treated with -e, pl = non-masculine plural.
BASE_GRAMMAR = {
    # Head
    "czapka": "f", "kask": "m", "helm": "m", "maska": "f", "obrecz": "f",
    "kominiarka": "f", "kapelusz": "m", "opaska": "f", "bandana": "f", "korona": "f",
    # Torso
    "koszulka": "f", "kurtka": "f", "marynarka": "f", "kamizelka": "f", "gorset": "m",
    "peleryna": "f", "smoking": "m", "kolczuga": "f", "zbroja warstwowa": "f", "pelna zbroja": "f",
    # Pants
    "szorty": "pl", "spodnie": "pl", "spodnica": "f", "kilt": "m",
    # Jewellery
    "pierscien": "m", "sygnet": "m", "bransoleta": "f",
    "naszyjnik": "m", "lancuch": "m", "amulet": "m", "krawat": "m", "apaszka": "f",
    # One-handed melee
    "palka": "f", "noz": "m", "sztylet": "m", "kastet": "m", "miecz": "m",
    "rapier": "m", "kama": "f", "topor": "m", "wakizashi": "n", "piesc niebios": "f",
    # Two-handed melee
    "maczuga": "f", "lom": "m", "miecz dwureczny": "m", "topor dwureczny": "m",
    "korbacz": "m", "kosa": "f", "pika": "f", "halabarda": "f", "katana": "f",
    "pila lancuchowa": "f",
    # Ranged
    "krotki luk": "m", "luk": "m", "shurek": "m", "dlugi luk": "m", "kusza": "f",
    "noz do rzucania": "m", "luk refleksyjny": "m", "oszczep": "m", "pilum": "n",
    "toporek do rzucania": "m", "ciezka kusza": "f",
    # Firearms
    "glock": "m", "beretta": "f", "uzi": "n", "magnum": "n", "desert eagle": "m",
    "mp5k": "n", "skorpion": "m", "karabin m.": "m", "polautomat s.": "m",
    "karabin s.": "m", "ak-47": "n", "fn-fal": "m", "strzelba": "f", "miotacz p.": "m",
}

BASE_DISPLAY_OVERRIDES = {
    "piesc niebios": "Pięść Niebios",
    "ak-47": "AK-47",
    "fn-fal": "FN-FAL",
    "mp5k": "MP5K",
    "uzi": "Uzi",
}


def semantic_prefix_key(value: str | None) -> str:
    value = value or ""
    return PREFIX_LEMMA_OVERRIDES.get(value, value)


def base_grammar(base: str | None) -> str:
    return BASE_GRAMMAR.get(base or "", "m")


def inflect_adjective(masculine: str, grammar: str) -> str:
    """Inflect a Polish adjective-like prefix from masculine singular.

    Moria item prefixes are adjective forms.  The simple ending rules below
    cover the catalogue used by the game; explicit semantic overrides above
    normalize plural/feminine matrix keys before inflection.
    """
    if not masculine or grammar == "m":
        return masculine
    if grammar not in {"f", "n", "pl"}:
        return masculine

    feminine = grammar == "f"
    if masculine.endswith(("ki", "gi")):
        return masculine[:-1] + ("a" if feminine else "e")
    if masculine.endswith("i"):
        return masculine + ("a" if feminine else "e")
    if masculine.endswith("y"):
        return masculine[:-1] + ("a" if feminine else "e")
    # Defensive fallback for a source value that is already in an -e form.
    if masculine.endswith("e"):
        return masculine
    return masculine


def legendary_word(base: str | None) -> str:
    grammar = base_grammar(base)
    if grammar == "f":
        return "Legendarna"
    if grammar in {"n", "pl"}:
        return "Legendarne"
    return "Legendarny"
