# 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 ConsentPolicy(BackboneElement):
    model_config = ConfigDict(serialize_by_alias=True, extra="forbid")

    authority: str | None = Field(
        None, alias="authority", serialization_alias="authority"
    )
    uri: str | None = Field(None, alias="uri", serialization_alias="uri")


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

    action: PyList[CodeableConcept] | None = Field(
        None, alias="action", serialization_alias="action"
    )
    actor: PyList[ConsentProvisionActor] | None = Field(
        None, alias="actor", serialization_alias="actor"
    )
    class_: PyList[Coding] | None = Field(
        None, alias="class", serialization_alias="class"
    )
    code: PyList[CodeableConcept] | None = Field(
        None, alias="code", serialization_alias="code"
    )
    data: PyList[ConsentProvisionData] | None = Field(
        None, alias="data", serialization_alias="data"
    )
    data_period: Period | None = Field(
        None, alias="dataPeriod", serialization_alias="dataPeriod"
    )
    period: Period | None = Field(None, alias="period", serialization_alias="period")
    provision: PyList[ConsentProvision] | None = Field(
        None, alias="provision", serialization_alias="provision"
    )
    purpose: PyList[Coding] | None = Field(
        None, alias="purpose", serialization_alias="purpose"
    )
    security_label: PyList[Coding] | None = Field(
        None, alias="securityLabel", serialization_alias="securityLabel"
    )
    type: Literal["deny", "permit"] | None = Field(
        None, alias="type", serialization_alias="type"
    )


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

    reference: Reference = Field(alias="reference", serialization_alias="reference")
    role: CodeableConcept = Field(alias="role", serialization_alias="role")


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

    meaning: Literal["instance", "related", "dependents", "authoredby"] = Field(
        alias="meaning", serialization_alias="meaning"
    )
    reference: Reference = Field(alias="reference", serialization_alias="reference")


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

    verification_date: str | None = Field(
        None, alias="verificationDate", serialization_alias="verificationDate"
    )
    verified: bool = Field(alias="verified", serialization_alias="verified")
    verified_with: Reference | None = Field(
        None, alias="verifiedWith", serialization_alias="verifiedWith"
    )


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

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

    category: PyList[CodeableConcept] = Field(
        alias="category", serialization_alias="category"
    )
    date_time: str | None = Field(
        None, alias="dateTime", serialization_alias="dateTime"
    )
    identifier: PyList[Identifier] | None = Field(
        None, alias="identifier", serialization_alias="identifier"
    )
    organization: PyList[Reference] | None = Field(
        None, alias="organization", serialization_alias="organization"
    )
    patient: Reference | None = Field(
        None, alias="patient", serialization_alias="patient"
    )
    performer: PyList[Reference] | None = Field(
        None, alias="performer", serialization_alias="performer"
    )
    policy: PyList[ConsentPolicy] | None = Field(
        None, alias="policy", serialization_alias="policy"
    )
    policy_rule: CodeableConcept | None = Field(
        None, alias="policyRule", serialization_alias="policyRule"
    )
    provision: ConsentProvision | None = Field(
        None, alias="provision", serialization_alias="provision"
    )
    scope: CodeableConcept = Field(alias="scope", serialization_alias="scope")
    source_attachment: Attachment | None = Field(
        None, alias="sourceAttachment", serialization_alias="sourceAttachment"
    )
    source_reference: Reference | None = Field(
        None, alias="sourceReference", serialization_alias="sourceReference"
    )
    status: Literal[
        "draft", "proposed", "active", "rejected", "inactive", "entered-in-error"
    ] = Field(alias="status", serialization_alias="status")
    verification: PyList[ConsentVerification] | None = Field(
        None, alias="verification", serialization_alias="verification"
    )

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