# 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, Identifier, Signature
from fhirschemapy.R4B.resource import Resource
from fhirschemapy.R4B.resource_families import ResourceFamily


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

    full_url: str | None = Field(None, alias="fullUrl", serialization_alias="fullUrl")
    link: PyList[BundleLink] | None = Field(
        None, alias="link", serialization_alias="link"
    )
    request: BundleEntryRequest | None = Field(
        None, alias="request", serialization_alias="request"
    )
    resource: ResourceFamily | None = Field(
        None, alias="resource", serialization_alias="resource"
    )
    response: BundleEntryResponse | None = Field(
        None, alias="response", serialization_alias="response"
    )
    search: BundleEntrySearch | None = Field(
        None, alias="search", serialization_alias="search"
    )


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

    if_match: str | None = Field(None, alias="ifMatch", serialization_alias="ifMatch")
    if_modified_since: str | None = Field(
        None, alias="ifModifiedSince", serialization_alias="ifModifiedSince"
    )
    if_none_exist: str | None = Field(
        None, alias="ifNoneExist", serialization_alias="ifNoneExist"
    )
    if_none_match: str | None = Field(
        None, alias="ifNoneMatch", serialization_alias="ifNoneMatch"
    )
    method: Literal["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"] = Field(
        alias="method", serialization_alias="method"
    )
    url: str = Field(alias="url", serialization_alias="url")


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

    etag: str | None = Field(None, alias="etag", serialization_alias="etag")
    last_modified: str | None = Field(
        None, alias="lastModified", serialization_alias="lastModified"
    )
    location: str | None = Field(None, alias="location", serialization_alias="location")
    outcome: ResourceFamily | None = Field(
        None, alias="outcome", serialization_alias="outcome"
    )
    status: str = Field(alias="status", serialization_alias="status")


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

    mode: Literal["match", "include", "outcome"] | None = Field(
        None, alias="mode", serialization_alias="mode"
    )
    score: float | None = Field(None, alias="score", serialization_alias="score")


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

    relation: str = Field(alias="relation", serialization_alias="relation")
    url: str = Field(alias="url", serialization_alias="url")


class Bundle(Resource):
    model_config = ConfigDict(serialize_by_alias=True, extra="forbid")

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

    entry: PyList[BundleEntry] | None = Field(
        None, alias="entry", serialization_alias="entry"
    )
    identifier: Identifier | None = Field(
        None, alias="identifier", serialization_alias="identifier"
    )
    link: PyList[BundleLink] | None = Field(
        None, alias="link", serialization_alias="link"
    )
    signature: Signature | None = Field(
        None, alias="signature", serialization_alias="signature"
    )
    timestamp: str | None = Field(
        None, alias="timestamp", serialization_alias="timestamp"
    )
    total: int | None = Field(None, alias="total", serialization_alias="total")
    type: Literal[
        "document",
        "message",
        "transaction",
        "transaction-response",
        "batch",
        "batch-response",
        "history",
        "searchset",
        "collection",
    ] = 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) -> Bundle:
        return cls.model_validate_json(json)
