from __future__ import annotations

from datetime import datetime

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

from supply_infra.db.base import Base


class DemandGradeCategoryRel(Base):
    """需求分级结果与全局树节点的归属关系，支撑按分类高效查询已分级需求。"""

    __tablename__ = "demand_grade_category_rel"
    __table_args__ = (
        UniqueConstraint(
            "demand_grade_id",
            "category_id",
            name="uk_demand_grade_category",
        ),
        Index("idx_demand_grade_id", "demand_grade_id"),
        Index("idx_category_id", "category_id"),
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    demand_grade_id: Mapped[int] = mapped_column(
        BigInteger, nullable=False, comment="demand_grade.id"
    )
    category_id: Mapped[int] = mapped_column(
        BigInteger, nullable=False, comment="global_tree_category.id"
    )
    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="更新时间",
    )
