"""UserCoupon model for tracking claimed coupons."""
from sqlmodel import SQLModel, Field, Relationship
from datetime import datetime
from typing import Optional, TYPE_CHECKING
from uuid import UUID, uuid4
from sqlalchemy import Column, DateTime, func, String
from enum import Enum

if TYPE_CHECKING:
    from app.models.user import User
    from app.models.coupon import Coupon

class UserCouponStatus(str, Enum):
    CLAIMED = "claimed"
    REDEEMED = "redeemed"
    EXPIRED = "expired"

class UserCoupon(SQLModel, table=True):
    __tablename__ = "user_coupons"
    
    id: Optional[UUID] = Field(default_factory=uuid4, primary_key=True)
    user_id: UUID = Field(foreign_key="users.id")
    coupon_id: UUID = Field(foreign_key="coupons.id")
    
    status: UserCouponStatus = Field(default=UserCouponStatus.CLAIMED)
    redemption_code: str = Field(
        sa_column=Column(String, unique=True, index=True)
    )
    
    claimed_at: datetime = Field(
        sa_column=Column(DateTime(timezone=True), server_default=func.now())
    )
    redeemed_at: Optional[datetime] = Field(
        sa_column=Column(DateTime(timezone=True), nullable=True)
    )
    
    # Relationships
    user: "User" = Relationship()
    coupon: "Coupon" = Relationship()

class UserCouponRead(SQLModel):
    id: UUID
    user_id: UUID
    coupon_id: UUID
    status: UserCouponStatus
    redemption_code: str
    claimed_at: datetime
    redeemed_at: Optional[datetime]
    coupon: Optional["Coupon"] = None
