"""
知识库 Schema
"""
from typing import Optional, List, Dict, Any
from datetime import datetime

from pydantic import BaseModel, Field, ConfigDict

from app.base_schema import CSTDatetime


class KnowledgeBaseCreate(BaseModel):
    """创建知识库"""
    model_config = ConfigDict(protected_namespaces=())
    
    application_id: Optional[str] = Field(None, description="所属应用ID")
    is_global: bool = Field(default=False, description="是否在子应用中可见")
    name: str = Field(..., max_length=100, description="知识库名称")
    code: str = Field(..., max_length=100, description="知识库编码")
    description: Optional[str] = Field(None, description="描述")
    icon: str = Field(default="", description="图标")
    embedding_model_id: Optional[str] = Field(None, description="Embedding 模型ID")
    embedding_dimensions: int = Field(default=1536, description="向量维度")
    chunk_strategy: str = Field(default="recursive", description="分块策略")
    chunk_size: int = Field(default=500, ge=100, le=4000, description="分块大小")
    chunk_overlap: int = Field(default=50, ge=0, le=500, description="分块重叠")
    separator: Optional[str] = Field(None, description="自定义分隔符")
    retrieval_mode: str = Field(default="hybrid", description="检索模式")
    top_k: int = Field(default=5, ge=1, le=20, description="检索数量")
    score_threshold: float = Field(default=0.5, ge=0, le=1, description="相似度阈值")
    rerank_enabled: bool = Field(default=False, description="是否启用重排序")
    rerank_model_id: Optional[str] = Field(None, description="重排序模型ID")
    retrieval_weight: float = Field(default=1.0, ge=0.1, le=10.0, description="检索权重")
    process_rules: Optional[Dict[str, Any]] = Field(None, description="预处理规则")
    indexing_technique: str = Field(default="high_quality", description="索引模式: high_quality/economy")


class KnowledgeBaseUpdate(BaseModel):
    """更新知识库"""
    model_config = ConfigDict(protected_namespaces=())
    
    name: Optional[str] = None
    description: Optional[str] = None
    icon: Optional[str] = None
    embedding_model_id: Optional[str] = None
    embedding_dimensions: Optional[int] = None
    chunk_strategy: Optional[str] = None
    chunk_size: Optional[int] = Field(None, ge=100, le=4000)
    chunk_overlap: Optional[int] = Field(None, ge=0, le=500)
    separator: Optional[str] = None
    retrieval_mode: Optional[str] = None
    top_k: Optional[int] = Field(None, ge=1, le=20)
    score_threshold: Optional[float] = Field(None, ge=0, le=1)
    rerank_enabled: Optional[bool] = None
    rerank_model_id: Optional[str] = None
    retrieval_weight: Optional[float] = Field(None, ge=0.1, le=10.0)
    process_rules: Optional[Dict[str, Any]] = None
    indexing_technique: Optional[str] = None
    status: Optional[str] = None
    is_global: Optional[bool] = None


class KnowledgeBaseResponse(BaseModel):
    """知识库详情输出"""
    id: str
    application_id: Optional[str] = None
    is_global: bool = False
    name: str
    code: str
    description: str = ""
    icon: str = ""
    embedding_model_id: Optional[str] = None
    embedding_model_name: str = ""
    embedding_dimensions: int = 1536
    chunk_strategy: str = "recursive"
    chunk_size: int = 500
    chunk_overlap: int = 50
    separator: Optional[str] = None
    retrieval_mode: str = "hybrid"
    top_k: int = 5
    score_threshold: float = 0.5
    rerank_enabled: bool = False
    rerank_model_id: Optional[str] = None
    retrieval_weight: float = 1.0
    process_rules: Optional[Dict[str, Any]] = None
    indexing_technique: str = "high_quality"
    document_count: int = 0
    segment_count: int = 0
    total_token_count: int = 0
    total_char_count: int = 0
    status: str = "active"
    sort: int = 0
    sys_create_datetime: Optional[CSTDatetime] = None
    sys_update_datetime: Optional[CSTDatetime] = None

    model_config = ConfigDict(from_attributes=True, protected_namespaces=())


class KnowledgeBaseListResponse(BaseModel):
    """知识库列表输出"""
    id: str
    application_id: Optional[str] = None
    application_name: str = ""
    is_global: bool = False
    name: str
    code: str
    description: str = ""
    icon: str = ""
    embedding_model_name: str = ""
    document_count: int = 0
    segment_count: int = 0
    status: str = "active"
    sys_create_datetime: Optional[CSTDatetime] = None

    model_config = ConfigDict(from_attributes=True, protected_namespaces=())
