"""
Pydantic schemas for diagnostic reports, pathologist verification, and amendments.
"""

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, Field

from app.schemas.result import ParameterResultInput


class ReportSubmitRequest(BaseModel):
    """Technician request submitting drafted findings for pathologist verification."""

    notes: str | None = Field(None, max_length=500, description="Optional submission note for the verifier")
    verifier_id: UUID | None = Field(None, description="Optional assigned pathologist / medical reviewer ID")


class ImagingSectionInput(BaseModel):
    """Per-study narrative for one imaging test on an order."""

    test_id: UUID = Field(..., description="Ordered imaging test this section belongs to")
    findings: str = Field(..., min_length=1, max_length=2000, description="Section remarks")
    impression: str = Field(..., min_length=1, max_length=1000, description="Section impression")


class ReportVerifyRequest(BaseModel):
    """Pathologist verification and electronic sign-off payload."""

    verification_reason: str | None = Field(
        "Clinically verified and authorized",
        max_length=500,
        description="Clinical impression, interpretation summary, or sign-off rationale",
    )
    clinical_notes: str | None = Field(
        None,
        max_length=1000,
        description="Additional pathology remarks or clinical notes to print on report",
    )
    imaging_sections: list[ImagingSectionInput] | None = Field(
        None, description="Per-study narrative; required to verify an imaging report"
    )


class ParameterRemark(BaseModel):
    """Per-analyte instruction from the pathologist for lab rework."""

    parameter_id: UUID = Field(..., description="Analyte the remark targets")
    remark: str = Field(..., min_length=1, max_length=500, description="Rework instruction for the bench")


class ReportRejectRequest(BaseModel):
    """Pathologist rejection of report draft with documented clinical rationale."""

    reason: str = Field(
        ...,
        min_length=3,
        max_length=500,
        description="Clinical, specimen quality, or analytical reason for rejection",
    )
    parameter_remarks: list[ParameterRemark] = Field(
        default_factory=list,
        description="Optional per-analyte rework instructions, shown on the bench worksheet",
    )


class ReportAmendRequest(BaseModel):
    """Request payload to create a new versioned amendment from a verified/released report."""

    reason: str = Field(
        ...,
        min_length=5,
        max_length=500,
        description="Forensic rationale for amending verified diagnostic findings",
    )
    results: list[ParameterResultInput] | None = Field(
        None,
        description="Optional updated or corrected parameter observations",
    )
    notes: str | None = Field(
        None,
        max_length=1000,
        description="Updated clinical impressions or amendment notes",
    )


class ReportReleaseRequest(BaseModel):
    """Payload to release a verified report to patient / ordering physician."""

    notes: str | None = Field(None, max_length=500, description="Release or dispatch notes")


class ReportResendRequest(BaseModel):
    """Payload to re-dispatch a released report to a (possibly corrected) email."""

    email: str = Field(..., min_length=3, max_length=255, description="Destination email address")


class ReportVerifierSummary(BaseModel):
    """Minimal summary of the medical professional who verified the report."""

    id: UUID
    name: str
    role: str


class ReportResponse(BaseModel):
    """Diagnostic report list item summary."""

    id: UUID
    order_id: UUID
    order_number: str
    patient_id: UUID
    patient_name: str
    version: int
    status: str
    verified_by: ReportVerifierSummary | None = None
    verification_reason: str | None = None
    verified_at: datetime | None = None
    released_at: datetime | None = None
    notes: str | None = None
    modality: str = Field(
        default="mixed",
        description="Order discipline mix: pathology (specimen lab only), imaging (radiology only), or mixed",
    )
    created_at: datetime
    updated_at: datetime


class ReportDetailResponse(ReportResponse):
    """Comprehensive diagnostic report response with complete observations and lineage."""

    previous_report_id: UUID | None = None
    file_url: str | None = None
    result_data: dict[str, object] | None = None
    notes: str | None = None
    public_token: str | None = None
