from __future__ import annotations

from datetime import datetime

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

from supply_infra.db.base import Base


class GlobalTreeElement(Base):
    """全局树元素 — 从 ODPS pattern_mining_element 同步。"""

    __tablename__ = "global_tree_element"
    __table_args__ = (
        UniqueConstraint("name", "category_id", name="global_tree_element_pk"),
        Index("idx_category_id", "category_id"),
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    name: Mapped[str | None] = mapped_column(String(128), nullable=True, comment="名称")
    category_id: Mapped[int] = mapped_column(BigInteger, nullable=False, comment="分类id")
    source_category_id: Mapped[int] = mapped_column(BigInteger, nullable=False, comment="hive表category_id")
    is_delete: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="是否删除 0-正常 1-删除")
    create_time: Mapped[datetime] = mapped_column(
        nullable=False,
        server_default=func.now(),
        comment="创建时间",
    )
    update_time: Mapped[datetime] = mapped_column(
        nullable=False,
        server_default=func.now(),
        onupdate=func.now(),
        comment="更新时间",
    )
