# core/protocol.py
from __future__ import annotations

from dataclasses import dataclass, field
from datetime import datetime


@dataclass
class Attachment:
    type: str  # "image" | "document" | "audio" | "video"
    url: str | None = None
    content: bytes | None = None
    filename: str | None = None
    mime_type: str | None = None
    workspace_path: str | None = None


@dataclass
class IncomingMessage:
    user_id: str
    platform: str
    chat_id: str
    text: str
    attachments: list[Attachment] = field(default_factory=list)
    reply_to: str | None = None


@dataclass
class IncomingCommand:
    user_id: str
    platform: str
    chat_id: str
    command: str
    args: list[str] = field(default_factory=list)


@dataclass
class IncomingCallback:
    user_id: str
    platform: str
    chat_id: str
    action: str
    payload: dict = field(default_factory=dict)


@dataclass
class OutgoingMessage:
    chat_id: str
    text: str
    parse_mode: str = "plain"
    attachments: list[Attachment] = field(default_factory=list)
    reply_to: str | None = None


@dataclass
class UIButton:
    label: str
    action: str
    payload: dict = field(default_factory=dict)
    style: str = "secondary"  # "primary" | "danger" | "secondary"


@dataclass
class OutgoingUI:
    chat_id: str
    text: str
    buttons: list[UIButton] = field(default_factory=list)


@dataclass
class OutgoingNotification:
    chat_id: str
    text: str
    level: str = "info"  # "info" | "warning" | "success" | "error"


@dataclass
class OutgoingTyping:
    chat_id: str
    is_typing: bool


@dataclass
class ChatContext:
    chat_id: str
    display_name: str
    platform: str
    surface_ref: str
    created_at: datetime
    is_archived: bool = False


@dataclass
class AuthFlow:
    user_id: str
    platform: str
    state: str  # "pending" | "code_sent" | "confirmed" | "failed"
    platform_user_id: str | None = None


@dataclass
class ConfirmationRequest:
    action_id: str
    chat_id: str
    description: str
    risk_level: str  # "low" | "medium" | "high"
    expires_at: datetime


@dataclass
class SettingsAction:
    action: str
    payload: dict = field(default_factory=dict)


@dataclass
class PaymentRequired:
    user_id: str
    reason: str
    current_plan: str


# Type aliases
IncomingEvent = IncomingMessage | IncomingCommand | IncomingCallback
OutgoingEvent = OutgoingMessage | OutgoingUI | OutgoingNotification | OutgoingTyping
