"""Scheduling & Booking schemas — Phase 9.4."""
from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, Field

from app.models.types import BookingStatus, LifecycleStatus, RecurrenceKind, SessionKind, WaitlistStatus
from app.schemas.common import ORMBase


class EquipmentCreate(BaseModel):
    sports_center_id: UUID
    branch_id: UUID | None = None
    facility_id: UUID | None = None
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    description: str | None = None
    status: LifecycleStatus = LifecycleStatus.ACTIVE
    quantity_total: int = 1
    quantity_available: int = 1
    attributes: dict | None = None


class EquipmentUpdate(BaseModel):
    name: str | None = Field(default=None, max_length=255)
    description: str | None = None
    status: LifecycleStatus | None = None
    quantity_total: int | None = None
    quantity_available: int | None = None
    attributes: dict | None = None


class EquipmentRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    branch_id: UUID | None
    facility_id: UUID | None
    code: str
    name: str
    description: str | None
    status: LifecycleStatus
    quantity_total: int
    quantity_available: int
    attributes: dict | None
    is_deleted: bool


class SportsSessionCreate(BaseModel):
    sports_center_id: UUID
    branch_id: UUID | None = None
    code: str = Field(max_length=50)
    title: str = Field(max_length=255)
    description: str | None = None
    session_kind: SessionKind = SessionKind.CLASS
    sport_id: UUID | None = None
    coach_id: UUID | None = None
    facility_id: UUID | None = None
    court_id: UUID | None = None
    room_id: UUID | None = None
    starts_at: datetime
    ends_at: datetime
    capacity: int = 0
    recurrence_kind: RecurrenceKind = RecurrenceKind.NONE
    recurrence_rule: dict | None = None
    status: LifecycleStatus = LifecycleStatus.ACTIVE
    metadata_json: dict | None = None


class SportsSessionUpdate(BaseModel):
    title: str | None = Field(default=None, max_length=255)
    description: str | None = None
    session_kind: SessionKind | None = None
    sport_id: UUID | None = None
    coach_id: UUID | None = None
    facility_id: UUID | None = None
    court_id: UUID | None = None
    room_id: UUID | None = None
    starts_at: datetime | None = None
    ends_at: datetime | None = None
    capacity: int | None = None
    recurrence_kind: RecurrenceKind | None = None
    recurrence_rule: dict | None = None
    status: LifecycleStatus | None = None
    metadata_json: dict | None = None
    version: int | None = None


class SportsSessionRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    branch_id: UUID | None
    code: str
    title: str
    description: str | None
    session_kind: SessionKind
    sport_id: UUID | None
    coach_id: UUID | None
    facility_id: UUID | None
    court_id: UUID | None
    room_id: UUID | None
    starts_at: datetime
    ends_at: datetime
    capacity: int
    booked_count: int
    waitlist_count: int
    recurrence_kind: RecurrenceKind
    recurrence_rule: dict | None
    status: LifecycleStatus
    metadata_json: dict | None
    version: int
    is_deleted: bool


class BookingCreate(BaseModel):
    sports_center_id: UUID
    session_id: UUID
    member_id: UUID
    membership_id: UUID | None = None
    notes: str | None = None


class BookingCancelRequest(BaseModel):
    cancellation_reason: str | None = Field(default=None, max_length=255)


class BookingRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    session_id: UUID
    member_id: UUID
    membership_id: UUID | None
    booking_number: str
    status: BookingStatus
    notes: str | None
    confirmed_at: datetime | None
    cancelled_at: datetime | None
    cancellation_reason: str | None
    version: int
    is_deleted: bool


class WaitingListCreate(BaseModel):
    sports_center_id: UUID
    session_id: UUID
    member_id: UUID
    notes: str | None = None


class WaitingListRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    session_id: UUID
    member_id: UUID
    position: int
    status: WaitlistStatus
    notes: str | None
    is_deleted: bool


class ResourceReservationCreate(BaseModel):
    sports_center_id: UUID
    session_id: UUID | None = None
    booking_id: UUID | None = None
    facility_id: UUID | None = None
    equipment_id: UUID | None = None
    starts_at: datetime
    ends_at: datetime
    quantity: int = 1
    notes: str | None = None


class ResourceReservationRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    session_id: UUID | None
    booking_id: UUID | None
    facility_id: UUID | None
    equipment_id: UUID | None
    starts_at: datetime
    ends_at: datetime
    quantity: int
    status: BookingStatus
    notes: str | None
    is_deleted: bool
