"""
样本中心基本信息数据库模型
"""
from sqlalchemy import Column, String, Text, Date, Enum, Integer
from sqlalchemy.dialects.mysql import CHAR, LONGTEXT, TINYINT
from app.models.base import BaseModel


class StandardBaseInfo(BaseModel):
    """施工标准规范知识库模型"""
    __tablename__ = "t_samp_standard_base_info"

    chinese_name = Column(String(255), nullable=False, comment="中文名称")
    english_name = Column(String(500), nullable=True, comment="英文名称")
    standard_number = Column(String(100), nullable=True, comment="标准编号，如：xxx 3223—2021")
    issuing_authority = Column(String(255), nullable=True, comment="发布单位")
    release_date = Column(Date, nullable=True, comment="发布日期")
    implementation_date = Column(Date, nullable=True, comment="实施日期")
    drafting_unit = Column(String(255), nullable=True, comment="主编单位")
    approving_department = Column(String(255), nullable=True, comment="批准部门")
    participating_units_1 = Column(String(255), nullable=True, comment="参编单位_1，可多条，建议用JSON数组或拆分多个字段；此处为第一个参编单位")
    participating_units_2 = Column(String(255), nullable=True, comment="参编单位_2，第二个参编单位")
    participating_units_3 = Column(String(255), nullable=True, comment="参编单位_3，第三个参编单位")
    participating_units_4 = Column(String(255), nullable=True, comment="参编单位_4，第四个参编单位")
    document_type = Column(String(50), nullable=True, comment="文件类型，枚举：法律法规、国家标准、行业标准、企业标准、规范性文件、技术规范")
    professional_field = Column(String(50), nullable=True, comment="专业领域，枚举：法律、参考规范、行政法规、部门规章")
    engineering_phase = Column(String(50), nullable=True, comment="工程阶段，待补充具体枚举值，如：规划、设计、施工、运维等")
    validity = Column(String(20), nullable=True, comment="时效性，枚举：现行、已废止、被替代")
    reference_basis_1 = Column(String(500), nullable=True, comment="参考依据_1")
    reference_basis_2 = Column(String(500), nullable=True, comment="参考依据_2")
    source_url = Column(String(500), nullable=True, comment="文件来源网址，如：http://www.xxx.com/xxx/xxx/xxx.pdf")
    file_id = Column(String(255), nullable=True, comment="文件ID（OSS上传后），OSS对象存储唯一标识，如：oss://bucket-name/path/to/file.pdf")
    file_url = Column(String(500), nullable=True, comment="文件URL")
    content = Column(LONGTEXT, nullable=True, comment="文件内容")

    def __repr__(self):
        return f"<StandardBaseInfo {self.chinese_name}>"


class ConstructionPlanBaseInfo(BaseModel):
    """施工方案知识库模型"""
    __tablename__ = "t_samp_construction_plan_base_info"

    plan_name = Column(String(255), nullable=False, comment="方案名称")
    project_name = Column(String(255), nullable=True, comment="工程名称")
    project_section = Column(String(100), nullable=True, comment="工程标段")
    compiling_unit = Column(String(255), nullable=True, comment="编制单位")
    compiling_date = Column(Date, nullable=True, comment="编制日期")
    plan_summary = Column(Text, nullable=True, comment="方案概述")
    compilation_basis_1 = Column(String(500), nullable=True, comment="编制依据_1")
    compilation_basis_2 = Column(String(500), nullable=True, comment="编制依据_2")
    compilation_basis_3 = Column(String(500), nullable=True, comment="编制依据_3")
    compilation_basis_4 = Column(String(500), nullable=True, comment="编制依据_4")
    compilation_basis_5 = Column(String(500), nullable=True, comment="编制依据_5")
    compilation_basis_6 = Column(String(500), nullable=True, comment="编制依据_6")
    compilation_basis_7 = Column(String(500), nullable=True, comment="编制依据_7")
    compilation_basis_8 = Column(String(500), nullable=True, comment="编制依据_8")
    compilation_basis_9 = Column(String(500), nullable=True, comment="编制依据_9")
    file_url = Column(String(500), nullable=True, comment="文件URL")
    content = Column(LONGTEXT, nullable=True, comment="文件内容")

    def __repr__(self):
        return f"<ConstructionPlanBaseInfo {self.plan_name}>"


class OfficeRegulations(BaseModel):
    """办公制度知识库模型"""
    __tablename__ = "t_samp_office_regulations"

    file_name = Column(String(255), nullable=False, comment="文件名称")
    issuing_department = Column(String(255), nullable=True, comment="发布部门")
    document_type = Column(String(50), nullable=True, comment="文件类型，枚举：采购、报销、审批等")
    publish_date = Column(Date, nullable=True, comment="发布日期")
    effective_start_date = Column(Date, nullable=True, comment="时效开始日期")
    effective_end_date = Column(Date, nullable=True, comment="时效结束日期，若为长期有效，可设为 NULL 或 9999-12-31")
    file_url = Column(String(500), nullable=True, comment="文件URL")
    content = Column(LONGTEXT, nullable=True, comment="文件内容")

    def __repr__(self):
        return f"<OfficeRegulations {self.file_name}>"


class TechnicalTermEntity(BaseModel):
    """施工标准规范专业术语实体模型"""
    __tablename__ = "t_samp_technical_term_entity"

    term_name = Column(String(255), nullable=False, comment="术语名称")
    term_definition = Column(Text, nullable=True, comment="术语定义")
    term_category = Column(String(100), nullable=True, comment="术语分类")
    professional_field = Column(String(100), nullable=True, comment="专业领域")
    source_standard = Column(String(255), nullable=True, comment="来源标准")
    english_name = Column(String(500), nullable=True, comment="英文名称")
    synonyms = Column(Text, nullable=True, comment="同义词（JSON格式存储）")
    related_terms = Column(Text, nullable=True, comment="相关术语（JSON格式存储）")
    usage_examples = Column(Text, nullable=True, comment="使用示例")
    notes = Column(Text, nullable=True, comment="备注")

    def __repr__(self):
        return f"<TechnicalTermEntity {self.term_name}>"


class DocumentMain(BaseModel):
    """文档记录表模型"""
    __tablename__ = "t_samp_document_main"

    id = Column(String(36), primary_key=True, comment="主键")
    source_type = Column(Enum('basis', 'work', 'job'), nullable=False, comment="所属类型")
    source_id = Column(String(36), nullable=False, comment="所属ID")
    title = Column(String(255), nullable=False, comment="文档名称")
    conversion_status = Column(Integer, nullable=False, default=0, comment="状态: 0-待转换, 1-转换中, 2-完成, 3-失败")
    whether_to_enter = Column(Integer, nullable=False, default=0, comment="是否入库: 0-未入库, 1-已入库")
    conversion_error = Column(Text, nullable=True, comment="错误原因详情")
    file_url = Column(String(500), nullable=True, comment="文件URL")
    md_url = Column(String(500), nullable=True, comment="Markdown文件URL")
    json_url = Column(String(500), nullable=True, comment="JSON文件URL")
    file_extension = Column(String(10), nullable=True, comment="后缀名")
    created_by = Column(String(36), nullable=False, comment="创建人")
    created_time = Column(DateTime, nullable=False, default=func.now(), comment="创建时间")
    updated_by = Column(String(36), nullable=False, comment="修改人")
    updated_time = Column(DateTime, nullable=False, default=func.now(), onupdate=func.now(), comment="修改时间")

    def __repr__(self):
        return f"<DocumentMain {self.title}>"


class DocCategory(BaseModel):
    """文档分类模型"""
    __tablename__ = "t_samp_doc_category"

    id = Column(Integer, primary_key=True, autoincrement=True, comment="分类ID")
    name = Column(String(100), nullable=False, comment="分类名称")
    parent_id = Column(Integer, default=0, comment="父分类ID")
    level = Column(Integer, default=1, comment="分类层级")
    sort_order = Column(Integer, default=0, comment="排序")
    is_active = Column(TINYINT, default=1, comment="是否激活")

    def __repr__(self):
        return f"<DocCategory {self.name}>"