"""
Pydantic schemas for promotion-related requests and responses.
"""
from pydantic import BaseModel, Field, field_validator
from typing import Optional
from datetime import datetime


class ValidatePromotionRequest(BaseModel):
    """Schema for validating a promotion code."""
    code: str = Field(..., min_length=1, max_length=50, description="Promotion code")
    booking_value: Optional[float] = Field(None, ge=0, description="Booking value/amount")
    booking_amount: Optional[float] = Field(None, ge=0, description="Booking amount (alias for booking_value)")
    
    @field_validator('code')
    @classmethod
    def validate_code(cls, v: str) -> str:
        """Validate promotion code format."""
        if not v or not v.strip():
            raise ValueError("Promotion code cannot be empty")
        return v.strip().upper()
    
    model_config = {
        "json_schema_extra": {
            "example": {
                "code": "SUMMER2024",
                "booking_value": 500.00
            }
        }
    }


class CreatePromotionRequest(BaseModel):
    """Schema for creating a promotion."""
    code: str = Field(..., min_length=1, max_length=50, description="Promotion code")
    name: str = Field(..., min_length=1, max_length=200, description="Promotion name")
    description: Optional[str] = Field(None, max_length=1000)
    discount_type: str = Field(..., description="Discount type: 'percentage' or 'fixed'")
    discount_value: float = Field(..., gt=0, description="Discount value")
    min_booking_amount: Optional[float] = Field(None, ge=0)
    max_discount_amount: Optional[float] = Field(None, ge=0)
    start_date: Optional[str] = Field(None, description="Start date (ISO format)")
    end_date: Optional[str] = Field(None, description="End date (ISO format)")
    usage_limit: Optional[int] = Field(None, ge=1)
    status: Optional[str] = Field("active", description="Status: 'active' or 'inactive'")
    
    @field_validator('discount_type')
    @classmethod
    def validate_discount_type(cls, v: str) -> str:
        """Validate discount type."""
        if v not in ['percentage', 'fixed']:
            raise ValueError("Discount type must be 'percentage' or 'fixed'")
        return v
    
    @field_validator('discount_value')
    @classmethod
    def validate_discount_value(cls, v: float, info) -> float:
        """Validate discount value based on type."""
        if 'discount_type' in info.data and info.data['discount_type'] == 'percentage':
            if v > 100:
                raise ValueError("Percentage discount cannot exceed 100%")
        return v
    
    @field_validator('code')
    @classmethod
    def validate_code(cls, v: str) -> str:
        """Validate promotion code format."""
        if not v or not v.strip():
            raise ValueError("Promotion code cannot be empty")
        return v.strip().upper()
    
    model_config = {
        "json_schema_extra": {
            "example": {
                "code": "SUMMER2024",
                "name": "Summer Sale",
                "description": "20% off all bookings",
                "discount_type": "percentage",
                "discount_value": 20.0,
                "min_booking_amount": 100.0,
                "max_discount_amount": 500.0,
                "start_date": "2024-06-01T00:00:00Z",
                "end_date": "2024-08-31T23:59:59Z",
                "usage_limit": 100,
                "status": "active"
            }
        }
    }


class UpdatePromotionRequest(BaseModel):
    """Schema for updating a promotion."""
    code: Optional[str] = Field(None, min_length=1, max_length=50)
    name: Optional[str] = Field(None, min_length=1, max_length=200)
    description: Optional[str] = Field(None, max_length=1000)
    discount_type: Optional[str] = None
    discount_value: Optional[float] = Field(None, gt=0)
    min_booking_amount: Optional[float] = Field(None, ge=0)
    max_discount_amount: Optional[float] = Field(None, ge=0)
    start_date: Optional[str] = None
    end_date: Optional[str] = None
    usage_limit: Optional[int] = Field(None, ge=1)
    status: Optional[str] = None
    
    @field_validator('discount_type')
    @classmethod
    def validate_discount_type(cls, v: Optional[str]) -> Optional[str]:
        """Validate discount type if provided."""
        if v and v not in ['percentage', 'fixed']:
            raise ValueError("Discount type must be 'percentage' or 'fixed'")
        return v
    
    model_config = {
        "json_schema_extra": {
            "example": {
                "name": "Updated Summer Sale",
                "discount_value": 25.0,
                "status": "active"
            }
        }
    }

