# 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,
    Dosage,
    Identifier,
    Quantity,
    Reference,
)
from fhirschemapy.R4B.domain_resource import DomainResource


class MedicationDispensePerformer(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"
    )


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

    reason: PyList[CodeableConcept] | None = Field(
        None, alias="reason", serialization_alias="reason"
    )
    responsible_party: PyList[Reference] | None = Field(
        None, alias="responsibleParty", serialization_alias="responsibleParty"
    )
    type: CodeableConcept | None = Field(None, alias="type", serialization_alias="type")
    was_substituted: bool = Field(
        alias="wasSubstituted", serialization_alias="wasSubstituted"
    )


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

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

    authorizing_prescription: PyList[Reference] | None = Field(
        None,
        alias="authorizingPrescription",
        serialization_alias="authorizingPrescription",
    )
    category: CodeableConcept | None = Field(
        None, alias="category", serialization_alias="category"
    )
    context: Reference | None = Field(
        None, alias="context", serialization_alias="context"
    )
    days_supply: Quantity | None = Field(
        None, alias="daysSupply", serialization_alias="daysSupply"
    )
    destination: Reference | None = Field(
        None, alias="destination", serialization_alias="destination"
    )
    detected_issue: PyList[Reference] | None = Field(
        None, alias="detectedIssue", serialization_alias="detectedIssue"
    )
    dosage_instruction: PyList[Dosage] | None = Field(
        None, alias="dosageInstruction", serialization_alias="dosageInstruction"
    )
    event_history: PyList[Reference] | None = Field(
        None, alias="eventHistory", serialization_alias="eventHistory"
    )
    identifier: PyList[Identifier] | None = Field(
        None, alias="identifier", serialization_alias="identifier"
    )
    location: Reference | None = Field(
        None, alias="location", serialization_alias="location"
    )
    medication_codeable_concept: CodeableConcept | None = Field(
        None,
        alias="medicationCodeableConcept",
        serialization_alias="medicationCodeableConcept",
    )
    medication_reference: Reference | None = Field(
        None, alias="medicationReference", serialization_alias="medicationReference"
    )
    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[MedicationDispensePerformer] | None = Field(
        None, alias="performer", serialization_alias="performer"
    )
    quantity: Quantity | None = Field(
        None, alias="quantity", serialization_alias="quantity"
    )
    receiver: PyList[Reference] | None = Field(
        None, alias="receiver", serialization_alias="receiver"
    )
    status: Literal[
        "preparation",
        "in-progress",
        "cancelled",
        "on-hold",
        "completed",
        "entered-in-error",
        "stopped",
        "declined",
        "unknown",
    ] = Field(alias="status", serialization_alias="status")
    status_reason_codeable_concept: CodeableConcept | None = Field(
        None,
        alias="statusReasonCodeableConcept",
        serialization_alias="statusReasonCodeableConcept",
    )
    status_reason_reference: Reference | None = Field(
        None, alias="statusReasonReference", serialization_alias="statusReasonReference"
    )
    subject: Reference | None = Field(
        None, alias="subject", serialization_alias="subject"
    )
    substitution: MedicationDispenseSubstitution | None = Field(
        None, alias="substitution", serialization_alias="substitution"
    )
    supporting_information: PyList[Reference] | None = Field(
        None, alias="supportingInformation", serialization_alias="supportingInformation"
    )
    type: CodeableConcept | None = Field(None, alias="type", serialization_alias="type")
    when_handed_over: str | None = Field(
        None, alias="whenHandedOver", serialization_alias="whenHandedOver"
    )
    when_prepared: str | None = Field(
        None, alias="whenPrepared", serialization_alias="whenPrepared"
    )

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