"""领域模型 - 值对象定义"""

from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Dict, Set, Any, Optional


@dataclass
class CaseInfo:
    """案例信息值对象"""
    case_id: str
    case_name: str
    chat_file: Path
    csv_files: List[Path] = field(default_factory=list)
    sample_questions: List[str] = field(default_factory=list)

    @classmethod
    def from_case_number(cls, case_number: str, chat_file: Path) -> "CaseInfo":
        """从案例编号创建"""
        return cls(
            case_id=f"case-{case_number}",
            case_name="",
            chat_file=chat_file,
        )

    def get_resource_name(self) -> str:
        """获取资源名称（统一格式）"""
        return f"{self.case_id} - {self.case_name}"


@dataclass
class TableInfo:
    """表信息"""
    fields: Set[str] = field(default_factory=set)
    sample_values: Dict[str, List[str]] = field(default_factory=dict)
    primary_key: Optional[str] = None
    relationships: List[str] = field(default_factory=list)
    order_fields: List[str] = field(default_factory=list)


@dataclass
class AnalysisResult:
    """分析结果值对象"""
    fields: Set[str] = field(default_factory=set)
    sample_values: Dict[str, List[str]] = field(default_factory=dict)
    sql_queries: List[str] = field(default_factory=list)
    table_name: Optional[str] = None
    tables: Dict[str, TableInfo] = field(default_factory=dict)
    questions: List[str] = field(default_factory=list)

    def get_primary_table(self) -> Optional[str]:
        """获取主表名"""
        return self.table_name or (list(self.tables.keys())[0] if self.tables else None)

    def has_multiple_tables(self) -> bool:
        """是否有多个表"""
        return len(self.tables) > 1
