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


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

    attachment: Attachment = Field(alias="attachment", serialization_alias="attachment")
    format: Coding | None = Field(None, alias="format", serialization_alias="format")


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

    encounter: PyList[Reference] | None = Field(
        None, alias="encounter", serialization_alias="encounter"
    )
    event: PyList[CodeableConcept] | None = Field(
        None, alias="event", serialization_alias="event"
    )
    facility_type: CodeableConcept | None = Field(
        None, alias="facilityType", serialization_alias="facilityType"
    )
    period: Period | None = Field(None, alias="period", serialization_alias="period")
    practice_setting: CodeableConcept | None = Field(
        None, alias="practiceSetting", serialization_alias="practiceSetting"
    )
    related: PyList[Reference] | None = Field(
        None, alias="related", serialization_alias="related"
    )
    source_patient_info: Reference | None = Field(
        None, alias="sourcePatientInfo", serialization_alias="sourcePatientInfo"
    )


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

    code: Literal["replaces", "transforms", "signs", "appends"] = Field(
        alias="code", serialization_alias="code"
    )
    target: Reference = Field(alias="target", serialization_alias="target")


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

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

    authenticator: Reference | None = Field(
        None, alias="authenticator", serialization_alias="authenticator"
    )
    author: PyList[Reference] | None = Field(
        None, alias="author", serialization_alias="author"
    )
    category: PyList[CodeableConcept] | None = Field(
        None, alias="category", serialization_alias="category"
    )
    content: PyList[DocumentReferenceContent] = Field(
        alias="content", serialization_alias="content"
    )
    context: DocumentReferenceContext | None = Field(
        None, alias="context", serialization_alias="context"
    )
    custodian: Reference | None = Field(
        None, alias="custodian", serialization_alias="custodian"
    )
    date: str | None = Field(None, alias="date", serialization_alias="date")
    description: str | None = Field(
        None, alias="description", serialization_alias="description"
    )
    doc_status: (
        Literal["preliminary", "final", "amended", "entered-in-error"] | None
    ) = Field(None, alias="docStatus", serialization_alias="docStatus")
    identifier: PyList[Identifier] | None = Field(
        None, alias="identifier", serialization_alias="identifier"
    )
    master_identifier: Identifier | None = Field(
        None, alias="masterIdentifier", serialization_alias="masterIdentifier"
    )
    relates_to: PyList[DocumentReferenceRelatesTo] | None = Field(
        None, alias="relatesTo", serialization_alias="relatesTo"
    )
    security_label: PyList[CodeableConcept] | None = Field(
        None, alias="securityLabel", serialization_alias="securityLabel"
    )
    status: Literal["current", "superseded", "entered-in-error"] = Field(
        alias="status", serialization_alias="status"
    )
    subject: Reference | None = Field(
        None, alias="subject", serialization_alias="subject"
    )
    type: 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) -> DocumentReference:
        return cls.model_validate_json(json)
