"""
Promo Code schemas
"""
from datetime import datetime
from pydantic import BaseModel, Field


# === Create/Update ===

class PromoCodeCreate(BaseModel):
    """Schema for creating a promo code"""
    code: str | None = Field(None, min_length=3, max_length=50)  # None = auto-generate
    coins_amount: int = Field(..., ge=1, le=100000)
    max_uses: int | None = Field(None, ge=1)  # None = unlimited
    valid_from: datetime | None = None
    valid_until: datetime | None = None


class PromoCodeUpdate(BaseModel):
    """Schema for updating a promo code"""
    is_active: bool | None = None
    max_uses: int | None = None
    valid_until: datetime | None = None


# === Response ===

class PromoCodeResponse(BaseModel):
    """Schema for promo code in responses"""
    id: int
    code: str
    coins_amount: int
    max_uses: int | None
    uses_count: int
    is_active: bool
    valid_from: datetime | None
    valid_until: datetime | None
    created_at: datetime
    created_by_nickname: str | None = None

    class Config:
        from_attributes = True


class PromoCodeRedemptionUser(BaseModel):
    """User info for redemption"""
    id: int
    nickname: str


class PromoCodeRedemptionResponse(BaseModel):
    """Schema for redemption record"""
    id: int
    user: PromoCodeRedemptionUser
    coins_awarded: int
    redeemed_at: datetime

    class Config:
        from_attributes = True


# === Redeem ===

class PromoCodeRedeemRequest(BaseModel):
    """Schema for redeeming a promo code"""
    code: str = Field(..., min_length=1, max_length=50)


class PromoCodeRedeemResponse(BaseModel):
    """Schema for redeem response"""
    success: bool
    coins_awarded: int
    new_balance: int
    message: str
