from sqlalchemy import BIGINT, TIMESTAMP, Double, Enum, Index, Integer, JSON, 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='数据条数')
    clean_rules: Mapped[dict | None] = mapped_column(JSON, nullable=True, comment='野值清洗规则')
    row_start: Mapped[int | None] = mapped_column(Integer, nullable=True, comment='数据行起始行号(1-based, 含)')
    row_end: Mapped[int | None] = mapped_column(Integer, nullable=True, comment='数据行结束行号(1-based, 含)')
    stored_name: Mapped[str | None] = mapped_column(String(255), nullable=True, unique=True, comment='合并后CSV存储文件名')
    file_path: Mapped[str | None] = mapped_column(String(255), nullable=True, comment='合并后CSV路径')
    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='排序')


class DataQualityConfig(Base):
    __tablename__ = 'data_quality_config'
    __table_args__ = (
        {'mysql_comment': '数据质量准确性配置表'},
    )

    id: Mapped[int] = mapped_column(BIGINT, primary_key=True, autoincrement=True)
    field_name: Mapped[str] = mapped_column(String(50), nullable=False, unique=True, comment='字段名')
    label: Mapped[str] = mapped_column(String(100), nullable=False, comment='显示名称')
    unit: Mapped[str] = mapped_column(String(20), nullable=False, server_default=text("''"), comment='单位')
    min_value: Mapped[float | None] = mapped_column(Double, nullable=True, comment='合规最小值')
    max_value: Mapped[float | None] = mapped_column(Double, nullable=True, 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'),
    )

