from __future__ import annotations

from dataclasses import dataclass
from typing import Optional


@dataclass(frozen=True)
class WeaponInfo:
    name: str
    price: int
    side: str
    category: str


_WEAPON_TABLE = {
    "glock": WeaponInfo(name="Glock-18", price=200, side="T", category="pistol"),
    "hkp2000": WeaponInfo(name="P2000", price=200, side="CT", category="pistol"),
    "usp_silencer": WeaponInfo(name="USP-S", price=200, side="CT", category="pistol"),
    "elite": WeaponInfo(name="Dual Berettas", price=300, side="Both", category="pistol"),
    "p250": WeaponInfo(name="P250", price=300, side="Both", category="pistol"),
    "tec9": WeaponInfo(name="Tec-9", price=500, side="T", category="pistol"),
    "fiveseven": WeaponInfo(name="Five-SeveN", price=500, side="CT", category="pistol"),
    "cz75a": WeaponInfo(name="CZ75-Auto", price=500, side="Both", category="pistol"),
    "revolver": WeaponInfo(name="R8 Revolver", price=600, side="Both", category="pistol"),
    "deagle": WeaponInfo(name="Desert Eagle", price=700, side="Both", category="pistol"),
    "mac10": WeaponInfo(name="MAC-10", price=1050, side="T", category="smg"),
    "mp9": WeaponInfo(name="MP9", price=1250, side="CT", category="smg"),
    "ump45": WeaponInfo(name="UMP-45", price=1200, side="Both", category="smg"),
    "bizon": WeaponInfo(name="PP-Bizon", price=1400, side="Both", category="smg"),
    "mp7": WeaponInfo(name="MP7", price=1500, side="Both", category="smg"),
    "mp5sd": WeaponInfo(name="MP5-SD", price=1500, side="Both", category="smg"),
    "nova": WeaponInfo(name="Nova", price=1050, side="Both", category="shotgun"),
    "mag7": WeaponInfo(name="MAG-7", price=1300, side="CT", category="shotgun"),
    "sawedoff": WeaponInfo(name="Sawed-Off", price=1100, side="T", category="shotgun"),
    "xm1014": WeaponInfo(name="XM1014", price=2000, side="Both", category="shotgun"),
    "galilar": WeaponInfo(name="Galil AR", price=1800, side="T", category="rifle"),
    "famas": WeaponInfo(name="FAMAS", price=2050, side="CT", category="rifle"),
    "ak47": WeaponInfo(name="AK-47", price=2700, side="T", category="rifle"),
    "m4a1": WeaponInfo(name="M4A4", price=2900, side="CT", category="rifle"),
    "m4a1_silencer": WeaponInfo(name="M4A1-S", price=2900, side="CT", category="rifle"),
    "aug": WeaponInfo(name="AUG", price=3300, side="CT", category="rifle"),
    "sg556": WeaponInfo(name="SG 553", price=3300, side="T", category="rifle"),
    "awp": WeaponInfo(name="AWP", price=4750, side="Both", category="sniper"),
    "scar20": WeaponInfo(name="SCAR-20", price=5000, side="CT", category="sniper"),
    "g3sg1": WeaponInfo(name="G3SG1", price=5000, side="T", category="sniper"),
    "negev": WeaponInfo(name="Negev", price=1700, side="Both", category="lmg"),
    "m249": WeaponInfo(name="M249", price=5200, side="Both", category="lmg"),
}

_ALIASES = {
    "weapon_glock": "glock",
    "weapon_hkp2000": "hkp2000",
    "weapon_usp_silencer": "usp_silencer",
    "weapon_elite": "elite",
    "weapon_p250": "p250",
    "weapon_tec9": "tec9",
    "weapon_fiveseven": "fiveseven",
    "weapon_cz75a": "cz75a",
    "weapon_revolver": "revolver",
    "weapon_deagle": "deagle",
    "weapon_mac10": "mac10",
    "weapon_mp9": "mp9",
    "weapon_ump45": "ump45",
    "weapon_bizon": "bizon",
    "weapon_mp7": "mp7",
    "weapon_mp5sd": "mp5sd",
    "weapon_nova": "nova",
    "weapon_mag7": "mag7",
    "weapon_sawedoff": "sawedoff",
    "weapon_xm1014": "xm1014",
    "weapon_galilar": "galilar",
    "weapon_famas": "famas",
    "weapon_ak47": "ak47",
    "weapon_m4a1": "m4a1",
    "weapon_m4a1_silencer": "m4a1_silencer",
    "weapon_aug": "aug",
    "weapon_sg556": "sg556",
    "weapon_awp": "awp",
    "weapon_scar20": "scar20",
    "weapon_g3sg1": "g3sg1",
    "weapon_negev": "negev",
    "weapon_m249": "m249",
    "m4a4": "m4a1",
    "m4a1-s": "m4a1_silencer",
    "m4a1s": "m4a1_silencer",
    "sg553": "sg556",
    "pp-bizon": "bizon",
}


def normalize_weapon_name(raw: Optional[str]) -> str:
    if not raw:
        return ""
    s = str(raw).strip().lower()
    if not s:
        return ""
    s = s.replace(" ", "").replace("\t", "").replace("\n", "")
    s = s.replace("weapon_", "weapon_")
    if s in _ALIASES:
        return _ALIASES[s]
    if s.startswith("weapon_") and s in _ALIASES:
        return _ALIASES[s]
    if s.startswith("weapon_"):
        s2 = s[len("weapon_") :]
        return _ALIASES.get(s2, s2)
    return _ALIASES.get(s, s)


def get_weapon_info(raw: Optional[str]) -> Optional[WeaponInfo]:
    key = normalize_weapon_name(raw)
    if not key:
        return None
    return _WEAPON_TABLE.get(key)


def get_weapon_price(raw: Optional[str]) -> Optional[int]:
    info = get_weapon_info(raw)
    return info.price if info else None

