"""Doctor panel DTOs — Phase 13.2."""
from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import VisitSessionStatus
from app.schemas.appointments import AppointmentRead
from app.schemas.common import ORMBase

_FORBID = ConfigDict(extra="forbid")


class VisitSessionCreate(BaseModel):
    model_config = _FORBID
    clinic_id: UUID
    doctor_id: UUID
    patient_id: UUID
    appointment_id: UUID
    code: str = Field(max_length=50)
    queue_position: int = Field(default=100, ge=1, le=10000)
    metadata_json: dict | None = None


class VisitSessionActionRequest(BaseModel):
    model_config = _FORBID
    version: int = Field(ge=1)
    reason: str | None = Field(default=None, max_length=500)


class VisitSessionRead(ORMBase):
    id: UUID
    tenant_id: UUID
    clinic_id: UUID
    doctor_id: UUID
    patient_id: UUID
    appointment_id: UUID
    code: str
    status: VisitSessionStatus
    queue_position: int
    started_at: datetime | None
    finished_at: datetime | None
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class VisitNoteCreate(BaseModel):
    model_config = _FORBID
    visit_session_id: UUID
    doctor_id: UUID
    code: str = Field(max_length=50)
    title: str = Field(max_length=255)
    body: str | None = None
    storage_file_ref: str | None = Field(default=None, max_length=255)
    metadata_json: dict | None = None


class VisitNoteUpdate(BaseModel):
    model_config = _FORBID
    title: str | None = Field(default=None, max_length=255)
    body: str | None = None
    storage_file_ref: str | None = Field(default=None, max_length=255)
    version: int = Field(ge=1)
    metadata_json: dict | None = None


class VisitNoteRead(ORMBase):
    id: UUID
    tenant_id: UUID
    visit_session_id: UUID
    doctor_id: UUID
    code: str
    title: str
    body: str | None
    storage_file_ref: str | None
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class DoctorDashboardRead(BaseModel):
    doctor_id: UUID
    queued_count: int
    in_progress_count: int
    completed_today_count: int
    upcoming_appointments: list[AppointmentRead]


class DoctorQueueRead(BaseModel):
    doctor_id: UUID
    items: list[VisitSessionRead]
