"""活动优惠券领取记录表"""
import enum
from datetime import datetime

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

from app.models.base import BaseModel


class CouponActivityClaimStatus(str, enum.Enum):
    CLAIMED = "claimed"    # 已领取
    USED = "used"          # 已使用
    EXPIRED = "expired"    # 已过期


class CouponActivityClaim(BaseModel):
    """用户从活动页面领取优惠券的记录。

    活动券的 claim_method 仍为 code，本表仅用于记录"谁在活动里领了这张券"，
    从而让 list_available_coupons 能把活动券纳入用户的可用券列表，
    并按券自身的 max_uses_per_user 控制领取次数。
    """

    __tablename__ = "coupon_activity_claims"

    promo_code_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("promo_codes.id", ondelete="CASCADE"), nullable=False, index=True,
    )
    user_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True,
    )
    # 领取来源活动 ID（Go 服务的 activities 表，不建外键，仅记录）
    activity_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
    status: Mapped[CouponActivityClaimStatus] = mapped_column(
        SAEnum(CouponActivityClaimStatus, values_callable=lambda e: [m.value for m in e]),
        default=CouponActivityClaimStatus.CLAIMED, nullable=False, index=True,
    )
    used_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
    order_id: Mapped[int | None] = mapped_column(
        Integer, ForeignKey("orders.id", ondelete="SET NULL"), nullable=True, index=True,
    )
