from pydantic import BaseModel, Field, field_validator, ConfigDict
from typing import Optional
from datetime import datetime
from enum import Enum

class NotificationType(str, Enum):
    BOOKING = "booking"
    PAYMENT = "payment"
    CANCELLATION = "cancellation"
    REMINDER = "reminder"
    PROMOTION = "promotion"
    SYSTEM = "system"
    REVIEW = "review"

class DeliveryMethod(str, Enum):
    IN_APP = "in_app"
    EMAIL = "email"
    SMS = "sms"
    PUSH = "push"

class DeliveryStatus(str, Enum):
    PENDING = "pending"
    SENT = "sent"
    FAILED = "failed"

class NotificationBase(BaseModel):
    user_id: str = Field(..., min_length=36, max_length=36)
    notification_type: NotificationType
    title: str = Field(..., min_length=1, max_length=255)
    message: str = Field(..., min_length=1)
    link_url: Optional[str] = Field(None, max_length=500)
    delivery_method: DeliveryMethod
    delivery_status: DeliveryStatus = DeliveryStatus.PENDING
    
    @field_validator("title")
    @classmethod
    def validate_title(cls, v: str) -> str:
        if not v or not v.strip():
            raise ValueError("Title cannot be empty or whitespace")
        return v.strip()
    
    @field_validator("message")
    @classmethod
    def validate_message(cls, v: str) -> str:
        if not v or not v.strip():
            raise ValueError("Message cannot be empty or whitespace")
        return v.strip()

class NotificationCreate(NotificationBase):
    pass

class NotificationUpdate(BaseModel):
    user_id: Optional[str] = Field(None, min_length=36, max_length=36)
    notification_type: Optional[NotificationType] = None
    title: Optional[str] = Field(None, min_length=1, max_length=255)
    message: Optional[str] = Field(None, min_length=1)
    link_url: Optional[str] = Field(None, max_length=500)
    is_read: Optional[bool] = None
    read_at: Optional[datetime] = None
    delivery_method: Optional[DeliveryMethod] = None
    delivery_status: Optional[DeliveryStatus] = None
    sent_at: Optional[datetime] = None
    
    @field_validator("title")
    @classmethod
    def validate_title(cls, v: Optional[str]) -> Optional[str]:
        if v is not None:
            if not v.strip():
                raise ValueError("Title cannot be empty or whitespace")
            return v.strip()
        return v
    
    @field_validator("message")
    @classmethod
    def validate_message(cls, v: Optional[str]) -> Optional[str]:
        if v is not None:
            if not v.strip():
                raise ValueError("Message cannot be empty or whitespace")
            return v.strip()
        return v

class NotificationResponse(NotificationBase):
    id: str
    is_read: bool
    read_at: Optional[datetime]
    sent_at: Optional[datetime]
    created_at: datetime
    updated_at: datetime
    
    model_config = ConfigDict(from_attributes=True)