# 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 (
    Age,
    BackboneElement,
    CodeableConcept,
    ContactDetail,
    DataRequirement,
    Duration,
    Expression,
    Identifier,
    Period,
    Quantity,
    Range,
    Reference,
    RelatedArtifact,
    Timing,
    TriggerDefinition,
    UsageContext,
)
from fhirschemapy.R4B.domain_resource import DomainResource


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

    action: PyList[PlanDefinitionAction] | None = Field(
        None, alias="action", serialization_alias="action"
    )
    cardinality_behavior: Literal["single", "multiple"] | None = Field(
        None, alias="cardinalityBehavior", serialization_alias="cardinalityBehavior"
    )
    code: PyList[CodeableConcept] | None = Field(
        None, alias="code", serialization_alias="code"
    )
    condition: PyList[PlanDefinitionActionCondition] | None = Field(
        None, alias="condition", serialization_alias="condition"
    )
    definition_canonical: str | None = Field(
        None, alias="definitionCanonical", serialization_alias="definitionCanonical"
    )
    definition_uri: str | None = Field(
        None, alias="definitionUri", serialization_alias="definitionUri"
    )
    description: str | None = Field(
        None, alias="description", serialization_alias="description"
    )
    documentation: PyList[RelatedArtifact] | None = Field(
        None, alias="documentation", serialization_alias="documentation"
    )
    dynamic_value: PyList[PlanDefinitionActionDynamicValue] | None = Field(
        None, alias="dynamicValue", serialization_alias="dynamicValue"
    )
    goal_id: PyList[str] | None = Field(
        None, alias="goalId", serialization_alias="goalId"
    )
    grouping_behavior: (
        Literal["visual-group", "logical-group", "sentence-group"] | None
    ) = Field(None, alias="groupingBehavior", serialization_alias="groupingBehavior")
    input: PyList[DataRequirement] | None = Field(
        None, alias="input", serialization_alias="input"
    )
    output: PyList[DataRequirement] | None = Field(
        None, alias="output", serialization_alias="output"
    )
    participant: PyList[PlanDefinitionActionParticipant] | None = Field(
        None, alias="participant", serialization_alias="participant"
    )
    precheck_behavior: Literal["yes", "no"] | None = Field(
        None, alias="precheckBehavior", serialization_alias="precheckBehavior"
    )
    prefix: str | None = Field(None, alias="prefix", serialization_alias="prefix")
    priority: Literal["routine", "urgent", "asap", "stat"] | None = Field(
        None, alias="priority", serialization_alias="priority"
    )
    reason: PyList[CodeableConcept] | None = Field(
        None, alias="reason", serialization_alias="reason"
    )
    related_action: PyList[PlanDefinitionActionRelatedAction] | None = Field(
        None, alias="relatedAction", serialization_alias="relatedAction"
    )
    required_behavior: Literal["must", "could", "must-unless-documented"] | None = (
        Field(None, alias="requiredBehavior", serialization_alias="requiredBehavior")
    )
    selection_behavior: (
        Literal[
            "any", "all", "all-or-none", "exactly-one", "at-most-one", "one-or-more"
        ]
        | None
    ) = Field(None, alias="selectionBehavior", serialization_alias="selectionBehavior")
    subject_codeable_concept: CodeableConcept | None = Field(
        None,
        alias="subjectCodeableConcept",
        serialization_alias="subjectCodeableConcept",
    )
    subject_reference: Reference | None = Field(
        None, alias="subjectReference", serialization_alias="subjectReference"
    )
    text_equivalent: str | None = Field(
        None, alias="textEquivalent", serialization_alias="textEquivalent"
    )
    timing_age: Age | None = Field(
        None, alias="timingAge", serialization_alias="timingAge"
    )
    timing_date_time: str | None = Field(
        None, alias="timingDateTime", serialization_alias="timingDateTime"
    )
    timing_duration: Duration | None = Field(
        None, alias="timingDuration", serialization_alias="timingDuration"
    )
    timing_period: Period | None = Field(
        None, alias="timingPeriod", serialization_alias="timingPeriod"
    )
    timing_range: Range | None = Field(
        None, alias="timingRange", serialization_alias="timingRange"
    )
    timing_timing: Timing | None = Field(
        None, alias="timingTiming", serialization_alias="timingTiming"
    )
    title: str | None = Field(None, alias="title", serialization_alias="title")
    transform: str | None = Field(
        None, alias="transform", serialization_alias="transform"
    )
    trigger: PyList[TriggerDefinition] | None = Field(
        None, alias="trigger", serialization_alias="trigger"
    )
    type: CodeableConcept | None = Field(None, alias="type", serialization_alias="type")


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

    expression: Expression | None = Field(
        None, alias="expression", serialization_alias="expression"
    )
    kind: Literal["applicability", "start", "stop"] = Field(
        alias="kind", serialization_alias="kind"
    )


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

    expression: Expression | None = Field(
        None, alias="expression", serialization_alias="expression"
    )
    path: str | None = Field(None, alias="path", serialization_alias="path")


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

    role: CodeableConcept | None = Field(None, alias="role", serialization_alias="role")
    type: Literal["patient", "practitioner", "related-person", "device"] = Field(
        alias="type", serialization_alias="type"
    )


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

    action_id: str = Field(alias="actionId", serialization_alias="actionId")
    offset_duration: Duration | None = Field(
        None, alias="offsetDuration", serialization_alias="offsetDuration"
    )
    offset_range: Range | None = Field(
        None, alias="offsetRange", serialization_alias="offsetRange"
    )
    relationship: Literal[
        "before-start",
        "before",
        "before-end",
        "concurrent-with-start",
        "concurrent",
        "concurrent-with-end",
        "after-start",
        "after",
        "after-end",
    ] = Field(alias="relationship", serialization_alias="relationship")


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

    addresses: PyList[CodeableConcept] | None = Field(
        None, alias="addresses", serialization_alias="addresses"
    )
    category: CodeableConcept | None = Field(
        None, alias="category", serialization_alias="category"
    )
    description: CodeableConcept = Field(
        alias="description", serialization_alias="description"
    )
    documentation: PyList[RelatedArtifact] | None = Field(
        None, alias="documentation", serialization_alias="documentation"
    )
    priority: CodeableConcept | None = Field(
        None, alias="priority", serialization_alias="priority"
    )
    start: CodeableConcept | None = Field(
        None, alias="start", serialization_alias="start"
    )
    target: PyList[PlanDefinitionGoalTarget] | None = Field(
        None, alias="target", serialization_alias="target"
    )


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

    detail_codeable_concept: CodeableConcept | None = Field(
        None, alias="detailCodeableConcept", serialization_alias="detailCodeableConcept"
    )
    detail_quantity: Quantity | None = Field(
        None, alias="detailQuantity", serialization_alias="detailQuantity"
    )
    detail_range: Range | None = Field(
        None, alias="detailRange", serialization_alias="detailRange"
    )
    due: Duration | None = Field(None, alias="due", serialization_alias="due")
    measure: CodeableConcept | None = Field(
        None, alias="measure", serialization_alias="measure"
    )


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

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

    action: PyList[PlanDefinitionAction] | None = Field(
        None, alias="action", serialization_alias="action"
    )
    approval_date: str | None = Field(
        None, alias="approvalDate", serialization_alias="approvalDate"
    )
    author: PyList[ContactDetail] | None = Field(
        None, alias="author", serialization_alias="author"
    )
    contact: PyList[ContactDetail] | None = Field(
        None, alias="contact", serialization_alias="contact"
    )
    copyright: str | None = Field(
        None, alias="copyright", serialization_alias="copyright"
    )
    date: str | None = Field(None, alias="date", serialization_alias="date")
    description: str | None = Field(
        None, alias="description", serialization_alias="description"
    )
    editor: PyList[ContactDetail] | None = Field(
        None, alias="editor", serialization_alias="editor"
    )
    effective_period: Period | None = Field(
        None, alias="effectivePeriod", serialization_alias="effectivePeriod"
    )
    endorser: PyList[ContactDetail] | None = Field(
        None, alias="endorser", serialization_alias="endorser"
    )
    experimental: bool | None = Field(
        None, alias="experimental", serialization_alias="experimental"
    )
    goal: PyList[PlanDefinitionGoal] | None = Field(
        None, alias="goal", serialization_alias="goal"
    )
    identifier: PyList[Identifier] | None = Field(
        None, alias="identifier", serialization_alias="identifier"
    )
    jurisdiction: PyList[CodeableConcept] | None = Field(
        None, alias="jurisdiction", serialization_alias="jurisdiction"
    )
    last_review_date: str | None = Field(
        None, alias="lastReviewDate", serialization_alias="lastReviewDate"
    )
    library: PyList[str] | None = Field(
        None, alias="library", serialization_alias="library"
    )
    name: str | None = Field(None, alias="name", serialization_alias="name")
    publisher: str | None = Field(
        None, alias="publisher", serialization_alias="publisher"
    )
    purpose: str | None = Field(None, alias="purpose", serialization_alias="purpose")
    related_artifact: PyList[RelatedArtifact] | None = Field(
        None, alias="relatedArtifact", serialization_alias="relatedArtifact"
    )
    reviewer: PyList[ContactDetail] | None = Field(
        None, alias="reviewer", serialization_alias="reviewer"
    )
    status: Literal["draft", "active", "retired", "unknown"] = Field(
        alias="status", serialization_alias="status"
    )
    subject_codeable_concept: CodeableConcept | None = Field(
        None,
        alias="subjectCodeableConcept",
        serialization_alias="subjectCodeableConcept",
    )
    subject_reference: Reference | None = Field(
        None, alias="subjectReference", serialization_alias="subjectReference"
    )
    subtitle: str | None = Field(None, alias="subtitle", serialization_alias="subtitle")
    title: str | None = Field(None, alias="title", serialization_alias="title")
    topic: PyList[CodeableConcept] | None = Field(
        None, alias="topic", serialization_alias="topic"
    )
    type: CodeableConcept | None = Field(None, alias="type", serialization_alias="type")
    url: str | None = Field(None, alias="url", serialization_alias="url")
    usage: str | None = Field(None, alias="usage", serialization_alias="usage")
    use_context: PyList[UsageContext] | None = Field(
        None, alias="useContext", serialization_alias="useContext"
    )
    version: str | None = Field(None, alias="version", serialization_alias="version")

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