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


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

    mode: Literal["personal", "professional", "legal", "official"] = Field(
        alias="mode", serialization_alias="mode"
    )
    party: Reference | None = Field(None, alias="party", serialization_alias="party")
    time: str | None = Field(None, alias="time", serialization_alias="time")


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

    code: PyList[CodeableConcept] | None = Field(
        None, alias="code", serialization_alias="code"
    )
    detail: PyList[Reference] | None = Field(
        None, alias="detail", serialization_alias="detail"
    )
    period: Period | None = Field(None, alias="period", serialization_alias="period")


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

    code: Literal["replaces", "transforms", "signs", "appends"] = Field(
        alias="code", serialization_alias="code"
    )
    target_identifier: Identifier | None = Field(
        None, alias="targetIdentifier", serialization_alias="targetIdentifier"
    )
    target_reference: Reference | None = Field(
        None, alias="targetReference", serialization_alias="targetReference"
    )


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

    author: PyList[Reference] | None = Field(
        None, alias="author", serialization_alias="author"
    )
    code: CodeableConcept | None = Field(None, alias="code", serialization_alias="code")
    empty_reason: CodeableConcept | None = Field(
        None, alias="emptyReason", serialization_alias="emptyReason"
    )
    entry: PyList[Reference] | None = Field(
        None, alias="entry", serialization_alias="entry"
    )
    focus: Reference | None = Field(None, alias="focus", serialization_alias="focus")
    mode: Literal["working", "snapshot", "changes"] | None = Field(
        None, alias="mode", serialization_alias="mode"
    )
    ordered_by: CodeableConcept | None = Field(
        None, alias="orderedBy", serialization_alias="orderedBy"
    )
    section: PyList[CompositionSection] | None = Field(
        None, alias="section", serialization_alias="section"
    )
    text: Narrative | None = Field(None, alias="text", serialization_alias="text")
    title: str | None = Field(None, alias="title", serialization_alias="title")


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

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

    attester: PyList[CompositionAttester] | None = Field(
        None, alias="attester", serialization_alias="attester"
    )
    author: PyList[Reference] = Field(alias="author", serialization_alias="author")
    category: PyList[CodeableConcept] | None = Field(
        None, alias="category", serialization_alias="category"
    )
    confidentiality: Literal["U", "L", "M", "N", "R", "V"] | None = Field(
        None, alias="confidentiality", serialization_alias="confidentiality"
    )
    custodian: Reference | None = Field(
        None, alias="custodian", serialization_alias="custodian"
    )
    date: str = Field(alias="date", serialization_alias="date")
    encounter: Reference | None = Field(
        None, alias="encounter", serialization_alias="encounter"
    )
    event: PyList[CompositionEvent] | None = Field(
        None, alias="event", serialization_alias="event"
    )
    identifier: Identifier | None = Field(
        None, alias="identifier", serialization_alias="identifier"
    )
    relates_to: PyList[CompositionRelatesTo] | None = Field(
        None, alias="relatesTo", serialization_alias="relatesTo"
    )
    section: PyList[CompositionSection] | None = Field(
        None, alias="section", serialization_alias="section"
    )
    status: Literal["preliminary", "final", "amended", "entered-in-error"] = Field(
        alias="status", serialization_alias="status"
    )
    subject: Reference | None = Field(
        None, alias="subject", serialization_alias="subject"
    )
    title: str = Field(alias="title", serialization_alias="title")
    type: CodeableConcept = Field(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) -> Composition:
        return cls.model_validate_json(json)
