"""方案分类模型。"""

from __future__ import annotations

from sqlalchemy import BigInteger, CheckConstraint, ForeignKey, Index, Integer, String, text
from sqlalchemy.orm import Mapped, mapped_column

from dms.database.base import BusinessTableMixin
from dms.extensions import db


class Category(BusinessTableMixin, db.Model):
    __tablename__ = "doc_category"
    __table_args__ = (
        CheckConstraint(
            "category_type IN ('SCENE', 'STYLE', 'SITUATION', 'VERSION', 'OTHER')",
            name="category_type",
        ),
        CheckConstraint(
            "status IN ('ENABLED', 'DISABLED')",
            name="category_status",
        ),
        Index(
            "ix_doc_category_parent_active_sort",
            "parent_id",
            "is_deleted",
            "sort_no",
        ),
        Index(
            "ix_doc_category_status_active",
            "status",
            "is_deleted",
        ),
        Index(
            "ix_doc_category_type_active",
            "category_type",
            "is_deleted",
        ),
    )

    parent_id: Mapped[int | None] = mapped_column(
        BigInteger,
        ForeignKey("doc_category.id", ondelete="RESTRICT"),
        nullable=True,
    )
    category_code: Mapped[str] = mapped_column(
        String(64),
        nullable=False,
        unique=True,
    )
    category_name: Mapped[str] = mapped_column(String(128), nullable=False)
    category_type: Mapped[str] = mapped_column(
        String(32),
        nullable=False,
        server_default=text("'OTHER'"),
    )
    category_path: Mapped[str] = mapped_column(String(1000), nullable=False)
    sort_no: Mapped[int] = mapped_column(
        Integer,
        nullable=False,
        server_default=text("0"),
    )
    document_count: Mapped[int] = mapped_column(
        Integer,
        nullable=False,
        server_default=text("0"),
    )
    status: Mapped[str] = mapped_column(
        String(20),
        nullable=False,
        server_default=text("'ENABLED'"),
    )
