"""优惠券积分商城商品表"""
import enum

from sqlalchemy import BigInteger, Boolean, ForeignKey, Integer, String, Text
from sqlalchemy import Enum as SAEnum
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import BaseModel


class CouponMallItemStatus(str, enum.Enum):
    ACTIVE = "active"
    PAUSED = "paused"
    EXPIRED = "expired"


class CouponMallItemScope(str, enum.Enum):
    PUBLIC = "public"                  # 全站公开
    AGENT_SUBORDINATES = "agent_subordinates"  # 仅发行者绑定的客户/下级可见
    SPECIFIC_USERS = "specific_users"  # 指定用户可见


class CouponMallItem(BaseModel):
    """积分商城中的优惠券商品，后续可扩展为通用积分商城商品。"""

    __tablename__ = "coupon_mall_items"

    promo_code_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("promo_codes.id", ondelete="CASCADE"), nullable=False, index=True,
        comment="关联优惠券ID",
    )
    issuer_user_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True,
        comment="发行者用户ID，0 表示平台官方",
    )
    # 商品展示信息
    title: Mapped[str | None] = mapped_column(String(200), nullable=True, comment="商品标题")
    subtitle: Mapped[str | None] = mapped_column(String(500), nullable=True, comment="商品副标题")
    cover_image: Mapped[str | None] = mapped_column(String(500), nullable=True, comment="封面图 URL")
    description: Mapped[str | None] = mapped_column(Text, nullable=True, comment="商品详情说明")
    # 领取所需积分
    points_price: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="领取所需积分")
    # 库存
    stock_total: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="总库存，0=不限")
    stock_remaining: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="剩余库存")
    # 状态与可见范围
    status: Mapped[CouponMallItemStatus] = mapped_column(
        SAEnum(CouponMallItemStatus, values_callable=lambda e: [m.value for m in e]),
        default=CouponMallItemStatus.ACTIVE, nullable=False, index=True,
    )
    scope: Mapped[CouponMallItemScope] = mapped_column(
        SAEnum(CouponMallItemScope, values_callable=lambda e: [m.value for m in e]),
        default=CouponMallItemScope.PUBLIC, nullable=False, index=True,
        comment="可见范围",
    )
    # 指定可见用户，JSON 数组；空=不限
    visible_user_ids: Mapped[str | None] = mapped_column(Text, nullable=True, comment="指定可见用户ID列表")
    sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="排序权重")
    category: Mapped[str | None] = mapped_column(String(50), nullable=True, index=True, comment="商城分类")
    # 限购与等级门槛
    max_claims_per_user: Mapped[int] = mapped_column(
        Integer, default=1, nullable=False, comment="每用户最多可领取次数，0=不限",
    )
    max_claims_per_user_per_day: Mapped[int] = mapped_column(
        Integer, default=0, nullable=False, comment="每用户每天最多可领取次数，0=不限",
    )
    required_member_level: Mapped[int] = mapped_column(
        Integer, default=0, nullable=False, comment="所需最低会员等级，0=不限",
    )
    required_member_level_name: Mapped[str | None] = mapped_column(
        String(50), nullable=True, comment="所需会员等级名称展示",
    )
    is_hot: Mapped[bool] = mapped_column(
        Boolean, default=False, nullable=False, comment="是否热门推荐",
    )
