"""Patient portal DTOs — Phase 13.4."""
from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.schemas.appointments import AppointmentCreate, AppointmentLifecycleRequest, AppointmentRead
from app.schemas.common import ORMBase

_FORBID = ConfigDict(extra="forbid")


class PatientPortalProfileCreate(BaseModel):
    model_config = _FORBID
    patient_id: UUID
    preferred_clinic_id: UUID | None = None
    preferred_language: str = Field(default="fa", max_length=16)
    emergency_contact_ref: str | None = Field(default=None, max_length=255)
    notification_prefs: dict | None = None
    metadata_json: dict | None = None


class PatientPortalProfileUpdate(BaseModel):
    model_config = _FORBID
    preferred_clinic_id: UUID | None = None
    preferred_language: str | None = Field(default=None, max_length=16)
    emergency_contact_ref: str | None = Field(default=None, max_length=255)
    notification_prefs: dict | None = None
    metadata_json: dict | None = None
    version: int = Field(ge=1)


class PatientPortalProfileRead(ORMBase):
    id: UUID
    tenant_id: UUID
    patient_id: UUID
    preferred_clinic_id: UUID | None
    preferred_language: str
    emergency_contact_ref: str | None
    notification_prefs: dict | None
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class PatientDocumentRefCreate(BaseModel):
    model_config = _FORBID
    code: str = Field(max_length=50)
    title: str = Field(max_length=255)
    storage_file_ref: str = Field(max_length=255)
    document_type: str | None = Field(default=None, max_length=50)
    metadata_json: dict | None = None


class PatientDocumentRefRead(ORMBase):
    id: UUID
    tenant_id: UUID
    patient_id: UUID
    code: str
    title: str
    storage_file_ref: str
    document_type: str | None
    metadata_json: dict | None
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class PatientNotificationPreferenceUpsert(BaseModel):
    model_config = _FORBID
    channel: str = Field(max_length=32)
    template_key: str = Field(max_length=100)
    enabled: bool = True
    metadata_json: dict | None = None


class PatientNotificationPreferenceRead(ORMBase):
    id: UUID
    tenant_id: UUID
    patient_id: UUID
    channel: str
    template_key: str
    enabled: bool
    metadata_json: dict | None
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class PatientPortalAppointmentBook(AppointmentCreate):
    pass


class PatientPortalAppointmentCancel(AppointmentLifecycleRequest):
    pass
