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


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

    quantity: Ratio | None = Field(
        None, alias="quantity", serialization_alias="quantity"
    )
    substance_codeable_concept: CodeableConcept | None = Field(
        None,
        alias="substanceCodeableConcept",
        serialization_alias="substanceCodeableConcept",
    )
    substance_reference: Reference | None = Field(
        None, alias="substanceReference", serialization_alias="substanceReference"
    )


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

    expiry: str | None = Field(None, alias="expiry", serialization_alias="expiry")
    identifier: Identifier | None = Field(
        None, alias="identifier", serialization_alias="identifier"
    )
    quantity: Quantity | None = Field(
        None, alias="quantity", serialization_alias="quantity"
    )


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

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

    category: PyList[CodeableConcept] | None = Field(
        None, alias="category", serialization_alias="category"
    )
    code: CodeableConcept = Field(alias="code", serialization_alias="code")
    description: str | None = Field(
        None, alias="description", serialization_alias="description"
    )
    identifier: PyList[Identifier] | None = Field(
        None, alias="identifier", serialization_alias="identifier"
    )
    ingredient: PyList[SubstanceIngredient] | None = Field(
        None, alias="ingredient", serialization_alias="ingredient"
    )
    instance: PyList[SubstanceInstance] | None = Field(
        None, alias="instance", serialization_alias="instance"
    )
    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) -> Substance:
        return cls.model_validate_json(json)
