from sqlalchemy import BIGINT, TIMESTAMP, Enum, Index, Integer, String, Text, text
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base


class Category(Base):
    __tablename__ = 'categories'
    __table_args__ = (
        Index('idx_type', 'type'),
        Index('idx_parent', 'parent_id'),
        {'mysql_comment': '数据分类表'},
    )

    id: Mapped[int] = mapped_column(BIGINT, primary_key=True, autoincrement=True)
    name: Mapped[str] = mapped_column(String(100), nullable=False, comment='分类名称')
    type: Mapped[str] = mapped_column(
        Enum('data_file', 'data_package', name='category_type_enum'),
        nullable=False,
        comment='分类类型',
    )
    parent_id: Mapped[int | None] = mapped_column(BIGINT, nullable=True, comment='父分类ID')
    sort_order: Mapped[int] = mapped_column(Integer, nullable=False, server_default=text('0'), comment='排序')
    created_at: Mapped[str] = mapped_column(TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP'))
    updated_at: Mapped[str] = mapped_column(
        TIMESTAMP,
        nullable=False,
        server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
    )


class DataFile(Base):
    __tablename__ = 'data_files'
    __table_args__ = (
        Index('idx_file_category', 'category_id'),
        {'mysql_comment': '数据文件表'},
    )

    id: Mapped[int] = mapped_column(BIGINT, primary_key=True, autoincrement=True)
    filename: Mapped[str] = mapped_column(String(255), nullable=False, comment='原始文件名')
    stored_name: Mapped[str] = mapped_column(String(255), nullable=False, unique=True, comment='存储文件名')
    file_path: Mapped[str] = mapped_column(String(255), nullable=False, comment='文件路径')
    category_id: Mapped[int | None] = mapped_column(BIGINT, nullable=True, comment='分类ID')
    data_count: Mapped[int] = mapped_column(Integer, nullable=False, server_default=text('0'), comment='数据条数')
    uploaded_at: Mapped[str] = mapped_column(TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP'))
    remark: Mapped[str | None] = mapped_column(Text, nullable=True)


class DataPackage(Base):
    __tablename__ = 'data_packages'
    __table_args__ = (
        Index('idx_pkg_category', 'category_id'),
        {'mysql_comment': '数据包表'},
    )

    id: Mapped[int] = mapped_column(BIGINT, primary_key=True, autoincrement=True)
    name: Mapped[str] = mapped_column(String(255), nullable=False, comment='数据包名称')
    category_id: Mapped[int | None] = mapped_column(BIGINT, nullable=True, comment='分类ID(data_package类型)')
    remark: Mapped[str | None] = mapped_column(Text, nullable=True, comment='备注')
    data_count: Mapped[int] = mapped_column(Integer, nullable=False, server_default=text('0'), comment='数据条数')
    created_at: Mapped[str] = mapped_column(TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP'))
    updated_at: Mapped[str] = mapped_column(
        TIMESTAMP,
        nullable=False,
        server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
    )


class DataPackageFile(Base):
    __tablename__ = 'data_package_files'
    __table_args__ = (
        Index('idx_dpf_package', 'package_id'),
        Index('idx_dpf_file', 'file_id'),
        {'mysql_comment': '数据包文件关联表'},
    )

    id: Mapped[int] = mapped_column(BIGINT, primary_key=True, autoincrement=True)
    package_id: Mapped[int] = mapped_column(BIGINT, nullable=False, comment='数据包ID')
    file_id: Mapped[int] = mapped_column(BIGINT, nullable=False, comment='数据文件ID')
    sort_order: Mapped[int] = mapped_column(Integer, nullable=False, server_default=text('0'), comment='排序')
