# WARNING: This file is autogenerated by FHIR Schema Codegen.
# https://github.com/fhir-schema/fhir-schema-codegen
# Any manual changes made to this file may be overwritten.

from __future__ import annotations
from pydantic import ConfigDict, Field
from typing import List as PyList, Literal

from fhirschemapy.R4B.base import (
    Annotation,
    BackboneElement,
    CodeableConcept,
    Identifier,
    Period,
    Quantity,
    Range,
    Ratio,
    Reference,
    SampledData,
    Timing,
)
from fhirschemapy.R4B.domain_resource import DomainResource


class ObservationComponent(BackboneElement):
    model_config = ConfigDict(serialize_by_alias=True, extra="forbid")

    code: CodeableConcept = Field(alias="code", serialization_alias="code")
    data_absent_reason: CodeableConcept | None = Field(
        None, alias="dataAbsentReason", serialization_alias="dataAbsentReason"
    )
    interpretation: PyList[CodeableConcept] | None = Field(
        None, alias="interpretation", serialization_alias="interpretation"
    )
    reference_range: PyList[ObservationReferenceRange] | None = Field(
        None, alias="referenceRange", serialization_alias="referenceRange"
    )
    value_boolean: bool | None = Field(
        None, alias="valueBoolean", serialization_alias="valueBoolean"
    )
    value_codeable_concept: CodeableConcept | None = Field(
        None, alias="valueCodeableConcept", serialization_alias="valueCodeableConcept"
    )
    value_date_time: str | None = Field(
        None, alias="valueDateTime", serialization_alias="valueDateTime"
    )
    value_integer: int | None = Field(
        None, alias="valueInteger", serialization_alias="valueInteger"
    )
    value_period: Period | None = Field(
        None, alias="valuePeriod", serialization_alias="valuePeriod"
    )
    value_quantity: Quantity | None = Field(
        None, alias="valueQuantity", serialization_alias="valueQuantity"
    )
    value_range: Range | None = Field(
        None, alias="valueRange", serialization_alias="valueRange"
    )
    value_ratio: Ratio | None = Field(
        None, alias="valueRatio", serialization_alias="valueRatio"
    )
    value_sampled_data: SampledData | None = Field(
        None, alias="valueSampledData", serialization_alias="valueSampledData"
    )
    value_string: str | None = Field(
        None, alias="valueString", serialization_alias="valueString"
    )
    value_time: str | None = Field(
        None, alias="valueTime", serialization_alias="valueTime"
    )


class ObservationReferenceRange(BackboneElement):
    model_config = ConfigDict(serialize_by_alias=True, extra="forbid")

    age: Range | None = Field(None, alias="age", serialization_alias="age")
    applies_to: PyList[CodeableConcept] | None = Field(
        None, alias="appliesTo", serialization_alias="appliesTo"
    )
    high: Quantity | None = Field(None, alias="high", serialization_alias="high")
    low: Quantity | None = Field(None, alias="low", serialization_alias="low")
    text: str | None = Field(None, alias="text", serialization_alias="text")
    type: CodeableConcept | None = Field(None, alias="type", serialization_alias="type")


class Observation(DomainResource):
    model_config = ConfigDict(serialize_by_alias=True, extra="forbid")

    resource_type: str = Field(
        default="Observation",
        alias="resourceType",
        serialization_alias="resourceType",
        frozen=True,
        pattern="Observation",
    )

    based_on: PyList[Reference] | None = Field(
        None, alias="basedOn", serialization_alias="basedOn"
    )
    body_site: CodeableConcept | None = Field(
        None, alias="bodySite", serialization_alias="bodySite"
    )
    category: PyList[CodeableConcept] | None = Field(
        None, alias="category", serialization_alias="category"
    )
    code: CodeableConcept = Field(alias="code", serialization_alias="code")
    component: PyList[ObservationComponent] | None = Field(
        None, alias="component", serialization_alias="component"
    )
    data_absent_reason: CodeableConcept | None = Field(
        None, alias="dataAbsentReason", serialization_alias="dataAbsentReason"
    )
    derived_from: PyList[Reference] | None = Field(
        None, alias="derivedFrom", serialization_alias="derivedFrom"
    )
    device: Reference | None = Field(None, alias="device", serialization_alias="device")
    effective_date_time: str | None = Field(
        None, alias="effectiveDateTime", serialization_alias="effectiveDateTime"
    )
    effective_instant: str | None = Field(
        None, alias="effectiveInstant", serialization_alias="effectiveInstant"
    )
    effective_period: Period | None = Field(
        None, alias="effectivePeriod", serialization_alias="effectivePeriod"
    )
    effective_timing: Timing | None = Field(
        None, alias="effectiveTiming", serialization_alias="effectiveTiming"
    )
    encounter: Reference | None = Field(
        None, alias="encounter", serialization_alias="encounter"
    )
    focus: PyList[Reference] | None = Field(
        None, alias="focus", serialization_alias="focus"
    )
    has_member: PyList[Reference] | None = Field(
        None, alias="hasMember", serialization_alias="hasMember"
    )
    identifier: PyList[Identifier] | None = Field(
        None, alias="identifier", serialization_alias="identifier"
    )
    interpretation: PyList[CodeableConcept] | None = Field(
        None, alias="interpretation", serialization_alias="interpretation"
    )
    issued: str | None = Field(None, alias="issued", serialization_alias="issued")
    method: CodeableConcept | None = Field(
        None, alias="method", serialization_alias="method"
    )
    note: PyList[Annotation] | None = Field(
        None, alias="note", serialization_alias="note"
    )
    part_of: PyList[Reference] | None = Field(
        None, alias="partOf", serialization_alias="partOf"
    )
    performer: PyList[Reference] | None = Field(
        None, alias="performer", serialization_alias="performer"
    )
    reference_range: PyList[ObservationReferenceRange] | None = Field(
        None, alias="referenceRange", serialization_alias="referenceRange"
    )
    specimen: Reference | None = Field(
        None, alias="specimen", serialization_alias="specimen"
    )
    status: Literal[
        "registered",
        "preliminary",
        "final",
        "amended",
        "cancelled",
        "entered-in-error",
        "unknown",
        "corrected",
    ] = Field(alias="status", serialization_alias="status")
    subject: Reference | None = Field(
        None, alias="subject", serialization_alias="subject"
    )
    value_boolean: bool | None = Field(
        None, alias="valueBoolean", serialization_alias="valueBoolean"
    )
    value_codeable_concept: CodeableConcept | None = Field(
        None, alias="valueCodeableConcept", serialization_alias="valueCodeableConcept"
    )
    value_date_time: str | None = Field(
        None, alias="valueDateTime", serialization_alias="valueDateTime"
    )
    value_integer: int | None = Field(
        None, alias="valueInteger", serialization_alias="valueInteger"
    )
    value_period: Period | None = Field(
        None, alias="valuePeriod", serialization_alias="valuePeriod"
    )
    value_quantity: Quantity | None = Field(
        None, alias="valueQuantity", serialization_alias="valueQuantity"
    )
    value_range: Range | None = Field(
        None, alias="valueRange", serialization_alias="valueRange"
    )
    value_ratio: Ratio | None = Field(
        None, alias="valueRatio", serialization_alias="valueRatio"
    )
    value_sampled_data: SampledData | None = Field(
        None, alias="valueSampledData", serialization_alias="valueSampledData"
    )
    value_string: str | None = Field(
        None, alias="valueString", serialization_alias="valueString"
    )
    value_time: str | None = Field(
        None, alias="valueTime", serialization_alias="valueTime"
    )

    def to_json(self, indent: int | None = None) -> str:
        return self.model_dump_json(
            exclude_unset=True, exclude_none=True, indent=indent
        )

    @classmethod
    def from_json(cls, json: str) -> Observation:
        return cls.model_validate_json(json)
