"""Pydantic schemas for rental requests."""
from pydantic import BaseModel, EmailStr, field_validator
from datetime import date


class RentalRequestItemCreate(BaseModel):
    equipment_id: int
    quantity: int = 1

    @field_validator("quantity")
    @classmethod
    def quantity_positive(cls, v: int) -> int:
        if v < 1:
            raise ValueError("quantity must be >= 1")
        return v


class RentalRequestCreate(BaseModel):
    event_name: str
    date_start: date
    date_end: date
    location: str | None = None
    person_count: int | None = None
    contact_name: str
    contact_company: str | None = None
    contact_email: EmailStr
    contact_phone: str | None = None
    contact_street: str | None = None
    contact_postalcode: str | None = None
    contact_city: str | None = None
    message: str | None = None
    items: list[RentalRequestItemCreate]

    @field_validator("date_end")
    @classmethod
    def date_end_after_start(cls, v: date, values) -> date:
        start = values.data.get("date_start")
        if start and v < start:
            raise ValueError("date_end must be >= date_start")
        return v

    @field_validator("items")
    @classmethod
    def items_not_empty(cls, v: list) -> list:
        if len(v) == 0:
            raise ValueError("items must not be empty")
        return v


class RentalRequestResponse(BaseModel):
    reference_number: str
    status: str
