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


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

    on_behalf_of: Reference | None = Field(
        None, alias="onBehalfOf", serialization_alias="onBehalfOf"
    )
    role: PyList[CodeableConcept] | None = Field(
        None, alias="role", serialization_alias="role"
    )
    type: CodeableConcept | None = Field(None, alias="type", serialization_alias="type")
    who: Reference = Field(alias="who", serialization_alias="who")


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

    agent: PyList[ProvenanceAgent] | None = Field(
        None, alias="agent", serialization_alias="agent"
    )
    role: Literal["derivation", "revision", "quotation", "source", "removal"] = Field(
        alias="role", serialization_alias="role"
    )
    what: Reference = Field(alias="what", serialization_alias="what")


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

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

    activity: CodeableConcept | None = Field(
        None, alias="activity", serialization_alias="activity"
    )
    agent: PyList[ProvenanceAgent] = Field(alias="agent", serialization_alias="agent")
    entity: PyList[ProvenanceEntity] | None = Field(
        None, alias="entity", serialization_alias="entity"
    )
    location: Reference | None = Field(
        None, alias="location", serialization_alias="location"
    )
    occurred_date_time: str | None = Field(
        None, alias="occurredDateTime", serialization_alias="occurredDateTime"
    )
    occurred_period: Period | None = Field(
        None, alias="occurredPeriod", serialization_alias="occurredPeriod"
    )
    policy: PyList[str] | None = Field(
        None, alias="policy", serialization_alias="policy"
    )
    reason: PyList[CodeableConcept] | None = Field(
        None, alias="reason", serialization_alias="reason"
    )
    recorded: str = Field(alias="recorded", serialization_alias="recorded")
    signature: PyList[Signature] | None = Field(
        None, alias="signature", serialization_alias="signature"
    )
    target: PyList[Reference] = Field(alias="target", serialization_alias="target")

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