"""
知识库模型
"""
from sqlalchemy import Column, String, Text, Integer, Float, Boolean, JSON

from app.base_model import BaseModel


class KnowledgeBase(BaseModel):
    """
    知识库
    
    管理文档集合，配置分块策略和检索参数
    """
    __tablename__ = "ai_knowledge_base"

    application_id = Column(String(21), nullable=True, index=True, comment="所属应用ID（逻辑外键关联core_application）")
    is_global = Column(Boolean, default=False, comment="是否在子应用中可见")
    name = Column(String(100), nullable=False, comment="知识库名称")
    code = Column(String(100), nullable=False, unique=True, comment="知识库编码")
    description = Column(Text, nullable=True, comment="描述")
    icon = Column(String(50), default="", comment="图标")

    # Embedding 配置
    embedding_model_id = Column(String(21), nullable=True, comment="Embedding 模型ID（逻辑外键关联ai_llm_model）")
    embedding_dimensions = Column(Integer, default=1536, comment="向量维度")

    # 分块策略
    chunk_strategy = Column(String(20), default="recursive", comment="分块策略: recursive/semantic/markdown/fixed")
    chunk_size = Column(Integer, default=500, comment="分块大小（字符数）")
    chunk_overlap = Column(Integer, default=50, comment="分块重叠大小（字符数）")
    separator = Column(String(50), nullable=True, comment="自定义分隔符")

    # 检索配置
    retrieval_mode = Column(String(20), default="hybrid", comment="检索模式: vector/fulltext/hybrid")
    top_k = Column(Integer, default=5, comment="检索返回数量")
    score_threshold = Column(Float, default=0.5, comment="相似度阈值（0-1）")
    rerank_enabled = Column(Boolean, default=False, comment="是否启用重排序")
    rerank_model_id = Column(String(21), nullable=True, comment="重排序模型ID")
    retrieval_weight = Column(Float, default=1.0, comment="检索权重（多知识库检索时的加权系数，0.1-10.0）")

    # 预处理规则
    process_rules = Column(JSON, nullable=True, comment="预处理规则（清洗配置）")

    # 索引模式
    indexing_technique = Column(String(20), default="high_quality", comment="索引模式: high_quality/economy")

    # 统计
    document_count = Column(Integer, default=0, comment="文档数量")
    segment_count = Column(Integer, default=0, comment="分段数量")
    total_token_count = Column(Integer, default=0, comment="总 Token 数")
    total_char_count = Column(Integer, default=0, comment="总字符数")

    # 状态
    status = Column(String(20), default="active", comment="状态: active/disabled")
