from __future__ import annotations

from datetime import datetime

from sqlalchemy import BigInteger, Integer, String, Text, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column

from supply_infra.db.base import Base


class GlobalTreeCategory(Base):
    """全局树分类 — 从 ODPS public_pattern_mining_category 同步。"""

    __tablename__ = "global_tree_category"
    __table_args__ = (UniqueConstraint("source_id", name="uk_source_id"),)

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    name: Mapped[str | None] = mapped_column(String(128), nullable=True, comment="名称")
    description: Mapped[str | None] = mapped_column(Text, nullable=True, comment="描述")
    level: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="层级")
    parent_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True, comment="父节点")
    source_id: Mapped[int] = mapped_column(BigInteger, nullable=False, comment="hive表id")
    source_parent_id: Mapped[int] = mapped_column(BigInteger, nullable=False, comment="hive表父id")
    is_delete: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="是否删除0-正常 1-删除")
    create_time: Mapped[datetime | None] = mapped_column(
        nullable=True,
        server_default=func.now(),
        comment="创建时间",
    )
    update_time: Mapped[datetime] = mapped_column(
        nullable=False,
        server_default=func.now(),
        onupdate=func.now(),
        comment="更新时间",
    )
