"""
FreeExile Character Inventory, Personal Stash, and Merchant Storage Type Definitions.
Strict typing with @dataclass(slots=True) following Engineering Standards 2026.
"""

from enum import Enum
from typing import Dict, List, Optional, Any
from dataclasses import dataclass, field
from world.primal_stones_crafting import ItemRarity, Affix


class ItemType(str, Enum):
    WEAPON = "WEAPON"
    ARMOR = "ARMOR"
    CURRENCY = "CURRENCY"       # Primal Bloodstones / Linh Thạch
    CONSUMABLE = "CONSUMABLE"   # Elixirs, scrolls, teleport charters
    RELIC = "RELIC"             # Savage bone relics, martial amulets
    GEM = "GEM"                 # Skill tags and mutator gems


class EquipmentSlot(str, Enum):
    MAIN_HAND = "MAIN_HAND"
    OFF_HAND = "OFF_HAND"
    SWAP_MAIN_HAND = "SWAP_MAIN_HAND"
    SWAP_OFF_HAND = "SWAP_OFF_HAND"
    HELMET = "HELMET"
    BODY_ARMOR = "BODY_ARMOR"
    GLOVES = "GLOVES"
    BOOTS = "BOOTS"
    AMULET = "AMULET"
    RING_1 = "RING_1"
    RING_2 = "RING_2"
    BELT = "BELT"


@dataclass(slots=True)
class InventoryItem:
    item_uuid: str
    item_id: str
    name: str
    item_type: ItemType
    rarity: ItemRarity = ItemRarity.PHAM_PHAM
    item_level: int = 1
    stack_count: int = 1
    max_stack: int = 1
    affixes: List[Affix] = field(default_factory=list)
    is_identified: bool = True
    is_corrupted: bool = False
    asking_price_currency: Optional[str] = None
    asking_price_amount: int = 0
    listing_id: Optional[str] = None
    is_locked: bool = False
    locked_by_tx: Optional[str] = None
    width: int = 1
    height: int = 1
    grid_x: Optional[int] = None
    grid_y: Optional[int] = None
    metadata: Dict[str, Any] = field(default_factory=dict)


@dataclass(slots=True)
class CharacterInventory:
    account_id: str
    character_id: str
    capacity: int = 40
    grid_width: int = 12
    grid_height: int = 5
    slots: Dict[int, InventoryItem] = field(default_factory=dict)
    equipment: Dict[EquipmentSlot, InventoryItem] = field(default_factory=dict)


class StashTabType(str, Enum):
    GENERAL = "GENERAL"
    CURRENCY = "CURRENCY"
    EQUIPMENT = "EQUIPMENT"
    RELIC = "RELIC"


@dataclass(slots=True)
class StashTab:
    tab_id: str
    name: str
    tab_type: StashTabType = StashTabType.GENERAL
    color_hex: str = "#d97706"
    capacity: int = 48
    grid_width: int = 12
    grid_height: int = 12
    slots: Dict[int, InventoryItem] = field(default_factory=dict)


@dataclass(slots=True)
class PersonalStash:
    account_id: str
    tabs: Dict[str, StashTab] = field(default_factory=dict)


@dataclass(slots=True)
class MerchantStashTab:
    account_id: str
    is_unlocked: bool = False
    unlocked_at: Optional[float] = None
    capacity: int = 30
    grid_width: int = 12
    grid_height: int = 12
    slots: Dict[int, InventoryItem] = field(default_factory=dict)
    tab_name: str = "Hắc Thị Niêm Yết"


@dataclass(slots=True)
class PlayerStorageContainer:
    account_id: str
    character_id: str
    inventory: CharacterInventory
    stash: PersonalStash
    merchant_stash: MerchantStashTab
