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


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

    alt_id: str | None = Field(None, alias="altId", serialization_alias="altId")
    location: Reference | None = Field(
        None, alias="location", serialization_alias="location"
    )
    media: Coding | None = Field(None, alias="media", serialization_alias="media")
    name: str | None = Field(None, alias="name", serialization_alias="name")
    network: AuditEventAgentNetwork | None = Field(
        None, alias="network", serialization_alias="network"
    )
    policy: PyList[str] | None = Field(
        None, alias="policy", serialization_alias="policy"
    )
    purpose_of_use: PyList[CodeableConcept] | None = Field(
        None, alias="purposeOfUse", serialization_alias="purposeOfUse"
    )
    requestor: bool = Field(alias="requestor", serialization_alias="requestor")
    role: PyList[CodeableConcept] | None = Field(
        None, alias="role", serialization_alias="role"
    )
    type: CodeableConcept | None = Field(None, alias="type", serialization_alias="type")
    who: Reference | None = Field(None, alias="who", serialization_alias="who")


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

    address: str | None = Field(None, alias="address", serialization_alias="address")
    type: Literal["1", "2", "3", "4", "5"] | None = Field(
        None, alias="type", serialization_alias="type"
    )


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

    description: str | None = Field(
        None, alias="description", serialization_alias="description"
    )
    detail: PyList[AuditEventEntityDetail] | None = Field(
        None, alias="detail", serialization_alias="detail"
    )
    lifecycle: Coding | None = Field(
        None, alias="lifecycle", serialization_alias="lifecycle"
    )
    name: str | None = Field(None, alias="name", serialization_alias="name")
    query: str | None = Field(None, alias="query", serialization_alias="query")
    role: Coding | None = Field(None, alias="role", serialization_alias="role")
    security_label: PyList[Coding] | None = Field(
        None, alias="securityLabel", serialization_alias="securityLabel"
    )
    type: Coding | None = Field(None, alias="type", serialization_alias="type")
    what: Reference | None = Field(None, alias="what", serialization_alias="what")


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

    type: str = Field(alias="type", serialization_alias="type")
    value_base64binary: str | None = Field(
        None, alias="valueBase64Binary", serialization_alias="valueBase64Binary"
    )
    value_string: str | None = Field(
        None, alias="valueString", serialization_alias="valueString"
    )


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

    observer: Reference = Field(alias="observer", serialization_alias="observer")
    site: str | None = Field(None, alias="site", serialization_alias="site")
    type: PyList[Coding] | None = Field(None, alias="type", serialization_alias="type")


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

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

    action: Literal["C", "R", "U", "D", "E"] | None = Field(
        None, alias="action", serialization_alias="action"
    )
    agent: PyList[AuditEventAgent] = Field(alias="agent", serialization_alias="agent")
    entity: PyList[AuditEventEntity] | None = Field(
        None, alias="entity", serialization_alias="entity"
    )
    outcome: Literal["0", "4", "8", "12"] | None = Field(
        None, alias="outcome", serialization_alias="outcome"
    )
    outcome_desc: str | None = Field(
        None, alias="outcomeDesc", serialization_alias="outcomeDesc"
    )
    period: Period | None = Field(None, alias="period", serialization_alias="period")
    purpose_of_event: PyList[CodeableConcept] | None = Field(
        None, alias="purposeOfEvent", serialization_alias="purposeOfEvent"
    )
    recorded: str = Field(alias="recorded", serialization_alias="recorded")
    source: AuditEventSource = Field(alias="source", serialization_alias="source")
    subtype: PyList[Coding] | None = Field(
        None, alias="subtype", serialization_alias="subtype"
    )
    type: Coding = 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) -> AuditEvent:
        return cls.model_validate_json(json)
