from sqlalchemy import BigInteger, String, Integer, UniqueConstraint, Index
from sqlalchemy.dialects.mysql import TINYINT
from sqlalchemy.orm import Mapped, mapped_column

from app.core.models.base import Base, TimestampMixin, TenantMixin


class StockStatus(Base, TenantMixin, TimestampMixin):
    __tablename__ = "stock_statuses"
    __table_args__ = (
        UniqueConstraint("tenant_id", "slug", name="uk_stock_statuses_tenant_slug"),
        Index("ix_stock_statuses_tenant", "tenant_id"),
        {
            "mysql_engine": "InnoDB",
            "mysql_charset": "utf8mb4",
            "mysql_collate": "utf8mb4_unicode_ci",
        },
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    name: Mapped[str] = mapped_column(String(50), nullable=False, comment="状态名称")
    name_en: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="英文名称")
    slug: Mapped[str] = mapped_column(String(30), nullable=False, comment="系统标识如 in_stock")
    allow_purchase: Mapped[int] = mapped_column(TINYINT(1), nullable=False, default=1, comment="是否允许购买")
    badge_color: Mapped[str | None] = mapped_column(String(20), nullable=True, comment="标签颜色 hex")
    badge_text: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="Store 显示文案")
    badge_text_en: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="Store 英文显示文案")
    sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
    is_default: Mapped[int] = mapped_column(TINYINT(1), nullable=False, default=0, comment="新商品默认状态")
    is_system: Mapped[int] = mapped_column(TINYINT(1), nullable=False, default=0, comment="系统预置不可删除")
    status_type: Mapped[str] = mapped_column(String(20), nullable=False, default="in_stock", comment="状态类型: in_stock / out_of_stock")
    is_type_default: Mapped[int] = mapped_column(TINYINT(1), nullable=False, default=0, comment="是否为该类型的默认状态")
