"""ZeliAI SDK 응답 데이터 클래스"""

from dataclasses import dataclass, field
from typing import List, Optional


# ─── Chat ─────────────────────────────────────────────


@dataclass
class UsageInfo:
    """토큰 사용량 정보"""
    prompt_tokens: int = 0
    completion_tokens: int = 0
    total_tokens: int = 0


@dataclass
class ChatMessage:
    """채팅 메시지"""
    role: str = "assistant"
    content: str = ""


@dataclass
class ChatChoice:
    """채팅 응답 선택지"""
    index: int = 0
    message: ChatMessage = field(default_factory=ChatMessage)
    finish_reason: str = "stop"


@dataclass
class ChatResponse:
    """채팅 응답"""
    id: str = ""
    object: str = "chat.completion"
    created: int = 0
    model: str = ""
    choices: List[ChatChoice] = field(default_factory=list)
    usage: UsageInfo = field(default_factory=UsageInfo)
    session_id: Optional[str] = None

    @property
    def content(self) -> str:
        """첫 번째 응답의 텍스트 내용"""
        if self.choices:
            return self.choices[0].message.content
        return ""

    def __str__(self):
        return self.content


# ─── Search ───────────────────────────────────────────


@dataclass
class SearchResult:
    """검색 결과 항목"""
    title: str = ""
    url: str = ""
    snippet: str = ""
    source: str = "web"


@dataclass
class SearchResponse:
    """검색 응답"""
    id: str = ""
    query: str = ""
    results: List[SearchResult] = field(default_factory=list)
    ai_summary: Optional[str] = None
    total_results: int = 0

    def __str__(self):
        return self.ai_summary or f"{self.total_results} results found"


# ─── Document QA ──────────────────────────────────────


@dataclass
class DocSource:
    """문서 소스 정보"""
    file_name: str = ""
    relevance: float = 0.0


@dataclass
class DocQAResponse:
    """문서 Q&A 응답"""
    id: str = ""
    answer: str = ""
    model: str = ""
    sources: List[DocSource] = field(default_factory=list)

    def __str__(self):
        return self.answer


# ─── Models ───────────────────────────────────────────


@dataclass
class ModelInfo:
    """모델 정보"""
    id: str = ""
    name: str = ""
    provider: str = ""
    description: str = ""
    max_tokens: int = 0
    supports_streaming: bool = True
    supports_search: bool = False


# ─── Usage ────────────────────────────────────────────


@dataclass
class AccountUsage:
    """계정 사용량"""
    company_name: str = ""
    plan: str = ""
    total_requests: int = 0
    total_tokens: int = 0
    rpm_limit: int = 0
    tpm_limit: int = 0
    is_active: bool = True
