"""数据模型定义."""
from __future__ import annotations

from dataclasses import dataclass, field
from typing import Optional


@dataclass
class ConnectionConfig:
    """单条连接配置"""
    name: str
    env: str
    description: Optional[str] = None
    url: str = ""
    username: str = ""
    password: str = ""


@dataclass
class SafetyConfig:
    """安全与行为配置（按 env）"""
    read_only: bool = False
    max_search_size: int = 100
    confirm_delete: bool = True
    allow_delete_index: bool = False
    allow_create_index: bool = False
    risk_check: bool = True


@dataclass
class EsVersionInfo:
    """ES 版本信息"""
    major: int  # 6, 7, 8
    minor: int
    patch: int

    @property
    def version(self) -> int:
        return self.major


@dataclass
class SessionState:
    """会话状态"""
    current_connection: Optional[str] = None
    env: Optional[str] = None
    es_version: Optional[int] = None
    es_version_info: Optional[EsVersionInfo] = None
    safety: Optional[SafetyConfig] = None


@dataclass
class ProfileData:
    """Profile 数据（持久化）"""
    last_connection: Optional[str] = None
    connections: dict[str, dict] = field(default_factory=dict)  # name -> {es_version, es_version_info}
    envs: dict[str, dict] = field(default_factory=dict)  # env -> {safety}


@dataclass
class ConnectionItem:
    """连接列表项"""
    name: str
    env: str
    description: Optional[str] = None


@dataclass
class SearchResult:
    """搜索结果"""
    hits: list[dict]
    total: int
    truncated: bool = False


@dataclass
class ScrollResult:
    """滚动查询结果"""
    hits: list[dict]
    scroll_id: Optional[str] = None
    took_ms: int = 0


@dataclass
class BulkResult:
    """批量操作结果"""
    succeeded: int
    failed: int
    errors: list[dict] = field(default_factory=list)
