from datetime import datetime
from typing import List
from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime, ForeignKey, Float
from sqlalchemy.orm import relationship, declarative_base

Base = declarative_base()

class KnowledgeBase(Base):
    __tablename__ = 'knowledge_base'

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(50), unique=True, index=True, nullable=False)
    description = Column(Text, nullable=True)
    tags = Column(String(200), nullable=True)
    creator = Column(String(100), nullable=True)
    file_count = Column(Integer, default=0)  # 文件数量
    is_deleted = Column(Integer, default=0)
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    # 建立与KnowledgeFile的一对多关系
    files = relationship('KnowledgeFile', back_populates='knowledge_base')

class KnowledgeFile(Base):
    __tablename__ = 'knowledge_file'

    id = Column(Integer, primary_key=True, index=True)
    knowledge_base_id = Column(Integer, ForeignKey('knowledge_base.id'), nullable=False)
    file_name = Column(String(255), nullable=False)
    file_size = Column(Float, nullable=False)  # 文件大小（MB）
    file_type = Column(String(10), nullable=False)  # 文件扩展名
    minio_url = Column(String(500), nullable=False)  # MinIO存储路径
    version = Column(String(50), nullable=True)
    author = Column(String(100), nullable=True)
    year = Column(Integer, nullable=True)
    page_count = Column(Integer, nullable=True)  # 文档页数
    creator = Column(String(100), nullable=True)  # 创建人
    knowledge_type = Column(String(50), nullable=True)  # 知识类型
    is_deleted = Column(Integer, default=0)
    status = Column(Integer, default=0) # 0启用 1 停用
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    # 建立与KnowledgeBase的多对一关系
    knowledge_base = relationship('KnowledgeBase', back_populates='files')