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


class ReviewBase(BaseModel):
    order_id: str = Field(..., min_length=36, max_length=36)
    reviewer_id: str = Field(..., min_length=36, max_length=36)
    seller_id: str = Field(..., min_length=36, max_length=36)
    rating: int = Field(..., ge=1, le=5)
    title: Optional[str] = Field(None, max_length=255)
    comment: Optional[str] = None
    is_verified_purchase: bool = True

    @field_validator("rating")
    @classmethod
    def validate_rating(cls, v: int) -> int:
        if not (1 <= v <= 5):
            raise ValueError("Review rating must be between 1 and 5")
        return v


class ReviewCreate(ReviewBase):
    pass


class ReviewUpdate(BaseModel):
    order_id: Optional[str] = Field(None, min_length=36, max_length=36)
    reviewer_id: Optional[str] = Field(None, min_length=36, max_length=36)
    seller_id: Optional[str] = Field(None, min_length=36, max_length=36)
    rating: Optional[int] = Field(None, ge=1, le=5)
    title: Optional[str] = Field(None, max_length=255)
    comment: Optional[str] = None
    seller_response: Optional[str] = None
    seller_response_at: Optional[datetime] = None
    is_verified_purchase: Optional[bool] = None

    @field_validator("rating")
    @classmethod
    def validate_rating(cls, v: Optional[int]) -> Optional[int]:
        if v is not None and not (1 <= v <= 5):
            raise ValueError("Review rating must be between 1 and 5")
        return v


class ReviewResponse(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: str
    order_id: str
    reviewer_id: str
    seller_id: str
    rating: int
    title: Optional[str]
    comment: Optional[str]
    seller_response: Optional[str]
    seller_response_at: Optional[datetime]
    is_verified_purchase: bool
    created_at: datetime
    updated_at: datetime


class ReviewDetailsResponse(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: str
    order_id: str
    reviewer_id: str
    seller_id: str
    rating: int
    title: Optional[str]
    comment: Optional[str]
    seller_response: Optional[str]
    seller_response_at: Optional[datetime]
    is_verified_purchase: bool
    created_at: datetime
    updated_at: datetime
    order: "OrderSummary"
    reviewer: "UserSummary"
    seller: "UserSummary"


class OrderSummary(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: str
    order_number: str


class UserSummary(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: str
    email: str


class UserProfileSummary(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    display_name: Optional[str]


class SellerProfileSummary(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    store_name: str