# 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 (
    Age,
    Annotation,
    BackboneElement,
    CodeableConcept,
    Identifier,
    Period,
    Range,
    Reference,
)
from fhirschemapy.R4B.domain_resource import DomainResource


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

    action: CodeableConcept | None = Field(
        None, alias="action", serialization_alias="action"
    )
    manipulated: Reference = Field(
        alias="manipulated", serialization_alias="manipulated"
    )


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

    actor: Reference = Field(alias="actor", serialization_alias="actor")
    function: CodeableConcept | None = Field(
        None, alias="function", serialization_alias="function"
    )
    on_behalf_of: Reference | None = Field(
        None, alias="onBehalfOf", serialization_alias="onBehalfOf"
    )


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

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

    asserter: Reference | None = Field(
        None, alias="asserter", serialization_alias="asserter"
    )
    based_on: PyList[Reference] | None = Field(
        None, alias="basedOn", serialization_alias="basedOn"
    )
    body_site: PyList[CodeableConcept] | None = Field(
        None, alias="bodySite", serialization_alias="bodySite"
    )
    category: CodeableConcept | None = Field(
        None, alias="category", serialization_alias="category"
    )
    code: CodeableConcept | None = Field(None, alias="code", serialization_alias="code")
    complication: PyList[CodeableConcept] | None = Field(
        None, alias="complication", serialization_alias="complication"
    )
    complication_detail: PyList[Reference] | None = Field(
        None, alias="complicationDetail", serialization_alias="complicationDetail"
    )
    encounter: Reference | None = Field(
        None, alias="encounter", serialization_alias="encounter"
    )
    focal_device: PyList[ProcedureFocalDevice] | None = Field(
        None, alias="focalDevice", serialization_alias="focalDevice"
    )
    follow_up: PyList[CodeableConcept] | None = Field(
        None, alias="followUp", serialization_alias="followUp"
    )
    identifier: PyList[Identifier] | None = Field(
        None, alias="identifier", serialization_alias="identifier"
    )
    instantiates_canonical: PyList[str] | None = Field(
        None, alias="instantiatesCanonical", serialization_alias="instantiatesCanonical"
    )
    instantiates_uri: PyList[str] | None = Field(
        None, alias="instantiatesUri", serialization_alias="instantiatesUri"
    )
    location: Reference | None = Field(
        None, alias="location", serialization_alias="location"
    )
    note: PyList[Annotation] | None = Field(
        None, alias="note", serialization_alias="note"
    )
    outcome: CodeableConcept | None = Field(
        None, alias="outcome", serialization_alias="outcome"
    )
    part_of: PyList[Reference] | None = Field(
        None, alias="partOf", serialization_alias="partOf"
    )
    performed_age: Age | None = Field(
        None, alias="performedAge", serialization_alias="performedAge"
    )
    performed_date_time: str | None = Field(
        None, alias="performedDateTime", serialization_alias="performedDateTime"
    )
    performed_period: Period | None = Field(
        None, alias="performedPeriod", serialization_alias="performedPeriod"
    )
    performed_range: Range | None = Field(
        None, alias="performedRange", serialization_alias="performedRange"
    )
    performed_string: str | None = Field(
        None, alias="performedString", serialization_alias="performedString"
    )
    performer: PyList[ProcedurePerformer] | None = Field(
        None, alias="performer", serialization_alias="performer"
    )
    reason_code: PyList[CodeableConcept] | None = Field(
        None, alias="reasonCode", serialization_alias="reasonCode"
    )
    reason_reference: PyList[Reference] | None = Field(
        None, alias="reasonReference", serialization_alias="reasonReference"
    )
    recorder: Reference | None = Field(
        None, alias="recorder", serialization_alias="recorder"
    )
    report: PyList[Reference] | None = Field(
        None, alias="report", serialization_alias="report"
    )
    status: Literal[
        "preparation",
        "in-progress",
        "not-done",
        "on-hold",
        "stopped",
        "completed",
        "entered-in-error",
        "unknown",
    ] = Field(alias="status", serialization_alias="status")
    status_reason: CodeableConcept | None = Field(
        None, alias="statusReason", serialization_alias="statusReason"
    )
    subject: Reference = Field(alias="subject", serialization_alias="subject")
    used_code: PyList[CodeableConcept] | None = Field(
        None, alias="usedCode", serialization_alias="usedCode"
    )
    used_reference: PyList[Reference] | None = Field(
        None, alias="usedReference", serialization_alias="usedReference"
    )

    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) -> Procedure:
        return cls.model_validate_json(json)
