"""Medical record DTOs — Phase 13.5."""
from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import EncounterStatus
from app.schemas.common import ORMBase

_FORBID = ConfigDict(extra="forbid")


class MedicalRecordCreate(BaseModel):
    model_config = _FORBID
    clinic_id: UUID
    patient_id: UUID
    code: str = Field(max_length=50)
    access_policy: dict | None = None
    metadata_json: dict | None = None


class MedicalRecordRead(ORMBase):
    id: UUID
    tenant_id: UUID
    clinic_id: UUID
    patient_id: UUID
    code: str
    access_policy: dict | None
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class EncounterCreate(BaseModel):
    model_config = _FORBID
    medical_record_id: UUID
    visit_session_id: UUID | None = None
    appointment_id: UUID | None = None
    doctor_id: UUID
    code: str = Field(max_length=50)
    chief_complaint: str | None = None
    clinical_summary: str | None = None
    metadata_json: dict | None = None


class EncounterUpdate(BaseModel):
    model_config = _FORBID
    chief_complaint: str | None = None
    clinical_summary: str | None = None
    metadata_json: dict | None = None
    version: int = Field(ge=1)


class EncounterFinalize(BaseModel):
    model_config = _FORBID
    version: int = Field(ge=1)
    addendum: str | None = None


class EncounterRead(ORMBase):
    id: UUID
    tenant_id: UUID
    medical_record_id: UUID
    visit_session_id: UUID | None
    appointment_id: UUID | None
    doctor_id: UUID
    code: str
    status: EncounterStatus
    chief_complaint: str | None
    clinical_summary: str | None
    finalized_at: datetime | None
    addendum: str | None
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class ClinicalItemCreate(BaseModel):
    model_config = _FORBID
    code: str = Field(max_length=50)
    metadata_json: dict | None = None


class ConditionCreate(ClinicalItemCreate):
    display: str = Field(max_length=255)
    clinical_status: str | None = Field(default=None, max_length=50)


class AllergyCreate(ClinicalItemCreate):
    substance: str = Field(max_length=255)
    severity: str | None = Field(default=None, max_length=50)


class MedicationCreate(ClinicalItemCreate):
    medication_code: str = Field(max_length=100)
    display: str = Field(max_length=255)
    dosage: str | None = Field(default=None, max_length=100)


class ImmunizationCreate(ClinicalItemCreate):
    vaccine_code: str = Field(max_length=100)
    administered_on: str | None = Field(default=None, max_length=32)
    storage_file_ref: str | None = Field(default=None, max_length=255)


class ConditionRead(ORMBase):
    id: UUID
    tenant_id: UUID
    medical_record_id: UUID
    code: str
    display: str
    clinical_status: str | None
    metadata_json: dict | None
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class AllergyRead(ORMBase):
    id: UUID
    tenant_id: UUID
    medical_record_id: UUID
    code: str
    substance: str
    severity: str | None
    metadata_json: dict | None
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class MedicationRead(ORMBase):
    id: UUID
    tenant_id: UUID
    medical_record_id: UUID
    code: str
    medication_code: str
    display: str
    dosage: str | None
    metadata_json: dict | None
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class ImmunizationRead(ORMBase):
    id: UUID
    tenant_id: UUID
    medical_record_id: UUID
    code: str
    vaccine_code: str
    administered_on: str | None
    storage_file_ref: str | None
    metadata_json: dict | None
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class MedicalRecordAccessRequest(BaseModel):
    model_config = _FORBID
    reason: str | None = Field(default=None, max_length=500)
