"""
Pydantic schemas for clinical test worksheets and parameter result entries.
"""

from decimal import Decimal
from uuid import UUID

from pydantic import BaseModel, Field


class ParameterWorksheetItem(BaseModel):
    """Parameter item in the collection worksheet tailored to patient demographics."""

    parameter_id: UUID
    parameter_name: str
    unit: str | None = None
    decimal_places: int = 2
    sort_order: int = 0
    method_note: str | None = None
    reference_low: Decimal | None = None
    reference_high: Decimal | None = None
    reference_text: str | None = None
    current_value: str | None = None
    flag: str | None = None
    is_abnormal: bool = False
    notes: str | None = None
    pathologist_remark: str | None = None


class TestWorksheetItem(BaseModel):
    """Test group with its diagnostic parameters in the worksheet."""

    order_test_id: UUID
    test_id: UUID
    test_name: str
    test_code: str
    result_type: str
    status: str
    parameters: list[ParameterWorksheetItem] = []


class OrderWorksheetResponse(BaseModel):
    """Complete lab worksheet for an order tailored to patient age and gender."""

    order_id: UUID
    order_number: str
    patient_id: UUID
    patient_name: str
    patient_age: int | None = None
    patient_gender: str | None = None
    tests: list[TestWorksheetItem] = []
    report_id: UUID | None = None
    report_status: str | None = None
    report_version: int | None = None
    verification_reason: str | None = None
    verified_by_name: str | None = None
    verified_by_id: UUID | None = None
    rejected_by_id: UUID | None = None
    is_dispatched: bool = False


class ParameterResultInput(BaseModel):
    """Individual parameter observation entered by technician."""

    parameter_id: UUID
    value: str = Field(..., min_length=1, max_length=200, description="Raw observation or numeric reading")
    notes: str | None = Field(None, max_length=500, description="Optional observation or instrument note")


class OrderResultEntryRequest(BaseModel):
    """Batch parameter entry payload for an order."""

    results: list[ParameterResultInput] = Field(..., min_length=1, description="List of parameter readings")
    notes: str | None = Field(
        None, max_length=1000, description="Clinical remarks or specimen condition notes"
    )


class EvaluatedParameterResult(BaseModel):
    """Evaluated analyte reading with demographic reference intervals and flags."""

    parameter_id: UUID
    parameter_name: str
    unit: str | None = None
    value: str
    reference_low: Decimal | None = None
    reference_high: Decimal | None = None
    reference_text: str | None = None
    flag: str = Field(..., description="N=Normal, L=Low, H=High, ABNORMAL=Text mismatch")
    is_abnormal: bool = False
    notes: str | None = None


class OrderResultEntryResponse(BaseModel):
    """Result entry confirmation and evaluation summary."""

    order_id: UUID
    order_number: str
    report_id: UUID
    report_status: str
    total_parameters_entered: int
    abnormal_count: int
    evaluated_results: list[EvaluatedParameterResult] = []
