"""
Data Transfer Objects and Protocol Types for FreeExile Chat Microservice.
Complies with GEMINI.md 2026 standards: Strict typing, @dataclass(slots=True, frozen=True).
"""

from __future__ import annotations

import time
from dataclasses import dataclass, field
from enum import IntEnum
from typing import Tuple


class ChatChannelType(IntEnum):
    """Enumeration of chat communication channels in FreeExile."""
    UNSPECIFIED = 0
    WORLD = 1       # Server-wide broadcast (rate-limited, level gated)
    ZONE = 2        # Spatial map / AOI cell local broadcast
    GUILD = 3       # Internal guild / sect communication
    PARTY = 4       # Active party / raid channel
    WHISPER = 5     # Private 1-to-1 direct messaging
    SYSTEM = 6      # Divine announcement / world boss / legendary craft
    RECRUIT = 7     # LFG / Party recruitment
    FEEDBACK = 8    # Direct feedback / GM assistance channel


@dataclass(slots=True, frozen=True)
class ItemAffixDTO:
    """Represents a single magical or elemental affix on an item."""
    affix_id: str
    stat_name: str
    stat_value: float
    is_implicit: bool = False


@dataclass(slots=True, frozen=True)
class ItemSnapshotDTO:
    """
    Cryptographically authenticated snapshot of an item linked into chat.
    Guarantees item legitimacy via HMAC-SHA256 signature.
    """
    item_uuid: str
    item_name_key: str
    rarity: int                 # 1: Pham, 2: Linh, 3: Cuc, 4: Than, 5: Thai Co
    element: int                # 1: Kim, 2: Moc, 3: Thuy, 4: Hoa, 5: Tho
    quality: int                # 0-20%
    item_level: int
    crafter_name: str
    hmac_signature: str
    created_at_ms: int = field(default_factory=lambda: int(time.time() * 1000))
    affixes: Tuple[ItemAffixDTO, ...] = field(default_factory=tuple)


@dataclass(slots=True, frozen=True)
class ChatMessageDTO:
    """Represents an immutable chat message broadcast within the cluster."""
    message_id: str
    sender_id: int
    sender_name: str
    sender_title: str
    sender_guild_tag: str
    channel: ChatChannelType
    target_id: int
    raw_content: str
    filtered_content: str
    timestamp_ms: int
    is_censored: bool = False
    moderation_risk_score: int = 0
    linked_items: Tuple[ItemSnapshotDTO, ...] = field(default_factory=tuple)
    zone_id: str = ""
    guild_id: str = ""
    party_id: str = ""


@dataclass(slots=True, frozen=True)
class SendChatRequestDTO:
    """Client request to dispatch a chat message."""
    sender_id: int
    sender_name: str
    sender_level: int
    channel: ChatChannelType
    content: str
    target_id: int = 0
    sender_title: str = ""
    sender_guild_tag: str = ""
    zone_id: str = ""
    guild_id: str = ""
    party_id: str = ""
    link_item_uuids: Tuple[str, ...] = field(default_factory=tuple)


@dataclass(slots=True, frozen=True)
class SendChatResponseDTO:
    """Response returned to the sender after chat ingestion."""
    success: bool
    error_message: str = ""
    message_id: str = ""
    cooldown_remaining_ms: int = 0
    filtered_content: str = ""
    risk_score: int = 0


@dataclass(slots=True, frozen=True)
class QueryItemSnapshotResponseDTO:
    """Response returned when an observer queries a linked item tooltip."""
    is_valid: bool
    item_snapshot: ItemSnapshotDTO | None = None
    error_message: str = ""
