# 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, PositiveInt
from typing import List as PyList, Literal

from fhirschemapy.R4B.base import (
    BackboneElement,
    CodeableConcept,
    Identifier,
    Period,
    Reference,
)
from fhirschemapy.R4B.domain_resource import DomainResource


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

    condition: Reference = Field(alias="condition", serialization_alias="condition")
    rank: PositiveInt | None = Field(None, alias="rank", serialization_alias="rank")
    role: CodeableConcept | None = Field(None, alias="role", serialization_alias="role")


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

    period: Period = Field(alias="period", serialization_alias="period")
    status: Literal[
        "planned",
        "waitlist",
        "active",
        "onhold",
        "finished",
        "cancelled",
        "entered-in-error",
    ] = Field(alias="status", serialization_alias="status")


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

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

    account: PyList[Reference] | None = Field(
        None, alias="account", serialization_alias="account"
    )
    care_manager: Reference | None = Field(
        None, alias="careManager", serialization_alias="careManager"
    )
    diagnosis: PyList[EpisodeOfCareDiagnosis] | None = Field(
        None, alias="diagnosis", serialization_alias="diagnosis"
    )
    identifier: PyList[Identifier] | None = Field(
        None, alias="identifier", serialization_alias="identifier"
    )
    managing_organization: Reference | None = Field(
        None, alias="managingOrganization", serialization_alias="managingOrganization"
    )
    patient: Reference = Field(alias="patient", serialization_alias="patient")
    period: Period | None = Field(None, alias="period", serialization_alias="period")
    referral_request: PyList[Reference] | None = Field(
        None, alias="referralRequest", serialization_alias="referralRequest"
    )
    status: Literal[
        "planned",
        "waitlist",
        "active",
        "onhold",
        "finished",
        "cancelled",
        "entered-in-error",
    ] = Field(alias="status", serialization_alias="status")
    status_history: PyList[EpisodeOfCareStatusHistory] | None = Field(
        None, alias="statusHistory", serialization_alias="statusHistory"
    )
    team: PyList[Reference] | None = Field(
        None, alias="team", serialization_alias="team"
    )
    type: PyList[CodeableConcept] | None = Field(
        None, alias="type", serialization_alias="type"
    )

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