"""
Pydantic schemas for notifications, audit logs, and public client links.
"""

from datetime import datetime
from typing import Any
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.notification import NotificationChannel, NotificationStatus


class NotificationResponse(BaseModel):
    """Staff audit response for a dispatched notification."""

    model_config = ConfigDict(from_attributes=True)

    id: UUID
    patient_id: UUID
    order_id: UUID | None = None
    report_id: UUID | None = None
    channel: NotificationChannel
    status: NotificationStatus
    recipient: str
    subject: str | None = None
    message: str
    sent_at: datetime | None = None
    error_message: str | None = None
    created_at: datetime
    updated_at: datetime


class NotificationSendRequest(BaseModel):
    """Staff request to trigger an ad-hoc or manual notification."""

    patient_id: UUID
    channel: NotificationChannel = NotificationChannel.EMAIL
    recipient: str = Field(..., min_length=3, max_length=255, description="Destination phone or email")
    subject: str | None = Field(None, max_length=255, description="Email subject or portal title")
    message: str = Field(..., min_length=1, description="Message content")
    order_id: UUID | None = None
    report_id: UUID | None = None


class ClientTokenResponse(BaseModel):
    """Metadata response when a client access token is generated."""

    token: str
    view_url: str
    expires_at: datetime


class ClientLabSummary(BaseModel):
    """Branded lab summary for client-facing views."""

    name: str
    address: str | None = None
    logo_url: str | None = None


class ClientPatientSummary(BaseModel):
    """Demographics summary visible to patient."""

    name: str
    patient_code: str
    gender: str
    age: int | None = None


class ClientReportDetailResponse(BaseModel):
    """Public patient view of an officially released diagnostic report."""

    report_id: UUID
    order_number: str
    version: int
    status: str
    verified_at: datetime | None = None
    released_at: datetime | None = None
    verifier_name: str | None = None
    patient: ClientPatientSummary
    lab: ClientLabSummary
    result_data: dict[str, Any] | None = None


class ClientAppointmentDetailResponse(BaseModel):
    """Public patient view of a confirmed appointment schedule."""

    appointment_id: UUID
    status: str
    booked_via: str
    start_at: datetime | None = None
    end_at: datetime | None = None
    patient: ClientPatientSummary
    lab: ClientLabSummary
    notes: str | None = None


class OrderTrackStage(BaseModel):
    """One step of the patient-visible order pipeline."""

    key: str
    label: str
    done: bool


class ReportTrackStatus(BaseModel):
    """Per-side document state for split (pathology/imaging) orders."""

    modality: str
    status: str
    version: int
    ready: bool


class ClientOrderTrackResponse(BaseModel):
    """Public order-status lookup guarded by the registered phone number."""

    order_number: str
    patient_first_name: str
    stages: list[OrderTrackStage]
    report_ready: bool
    documents: list[ReportTrackStatus] = Field(default_factory=list)
