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


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

    expiration_date: str | None = Field(
        None, alias="expirationDate", serialization_alias="expirationDate"
    )
    lot_number: str | None = Field(
        None, alias="lotNumber", serialization_alias="lotNumber"
    )


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

    is_active: bool | None = Field(
        None, alias="isActive", serialization_alias="isActive"
    )
    item_codeable_concept: CodeableConcept | None = Field(
        None, alias="itemCodeableConcept", serialization_alias="itemCodeableConcept"
    )
    item_reference: Reference | None = Field(
        None, alias="itemReference", serialization_alias="itemReference"
    )
    strength: Ratio | None = Field(
        None, alias="strength", serialization_alias="strength"
    )


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

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

    amount: Ratio | None = Field(None, alias="amount", serialization_alias="amount")
    batch: MedicationBatch | None = Field(
        None, alias="batch", serialization_alias="batch"
    )
    code: CodeableConcept | None = Field(None, alias="code", serialization_alias="code")
    form: CodeableConcept | None = Field(None, alias="form", serialization_alias="form")
    identifier: PyList[Identifier] | None = Field(
        None, alias="identifier", serialization_alias="identifier"
    )
    ingredient: PyList[MedicationIngredient] | None = Field(
        None, alias="ingredient", serialization_alias="ingredient"
    )
    manufacturer: Reference | None = Field(
        None, alias="manufacturer", serialization_alias="manufacturer"
    )
    status: Literal["active", "inactive", "entered-in-error"] | None = Field(
        None, alias="status", serialization_alias="status"
    )

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