"""Appointment DTOs — Phase 14.1."""
from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import AppointmentLifecycleAction, AppointmentStatus
from app.schemas.common import ORMBase

_FORBID = ConfigDict(extra="forbid")


class AppointmentCreate(BaseModel):
    model_config = _FORBID

    organization_id: UUID
    branch_id: UUID
    appointment_type_id: UUID | None = None
    code: str = Field(max_length=50)
    customer_ref: str | None = None
    staff_ref: str | None = None
    room_ref: UUID | None = None
    service_ref: UUID | None = None
    status: AppointmentStatus = AppointmentStatus.REQUESTED
    scheduled_start: datetime
    scheduled_end: datetime
    notes: str | None = None
    metadata_json: dict | None = None


class AppointmentUpdate(BaseModel):
    model_config = _FORBID

    staff_ref: str | None = None
    room_ref: UUID | None = None
    service_ref: UUID | None = None
    scheduled_start: datetime | None = None
    scheduled_end: datetime | None = None
    notes: str | None = None
    metadata_json: dict | None = None
    version: int = Field(ge=1)


class AppointmentRead(ORMBase):
    id: UUID
    tenant_id: UUID
    organization_id: UUID
    branch_id: UUID
    appointment_type_id: UUID | None
    code: str
    customer_ref: str | None
    staff_ref: str | None
    room_ref: UUID | None
    service_ref: UUID | None
    status: AppointmentStatus
    scheduled_start: datetime
    scheduled_end: datetime
    notes: str | None
    status_reason: str | None
    status_changed_at: datetime | None
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class AppointmentLifecycleRequest(BaseModel):
    model_config = _FORBID

    reason: str | None = Field(default=None, max_length=500)
    version: int = Field(ge=1)


class AppointmentStatusHistoryRead(ORMBase):
    id: UUID
    tenant_id: UUID
    appointment_id: UUID
    action: AppointmentLifecycleAction
    from_status: AppointmentStatus
    to_status: AppointmentStatus
    reason: str | None
    actor_user_id: str | None
    metadata_json: dict | None
    created_at: datetime
