"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
# @generated-id: e6ae99849717

from __future__ import annotations
from .status_reason_code_response import StatusReasonCodeResponse
from mollie import models
from mollie.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from pydantic import field_serializer, model_serializer
from typing import Optional
from typing_extensions import NotRequired, TypedDict


class StatusReason2TypedDict(TypedDict):
    r"""A human-readable reason explaining the current status of the transfer. Only populated when the transfer has
    reached a terminal status of `failed`, `rejected`, or `blocked`.

    This field is `null` for transfers that have not reached one of these statuses.
    """

    code: NotRequired[StatusReasonCodeResponse]
    r"""A machine-readable code indicating the reason for the transfer's terminal status."""
    message: NotRequired[Nullable[str]]
    r"""Provides further details about failure indicated. This field is only populated if the`code` field is set to `error`."""


class StatusReason2(BaseModel):
    r"""A human-readable reason explaining the current status of the transfer. Only populated when the transfer has
    reached a terminal status of `failed`, `rejected`, or `blocked`.

    This field is `null` for transfers that have not reached one of these statuses.
    """

    code: Optional[StatusReasonCodeResponse] = None
    r"""A machine-readable code indicating the reason for the transfer's terminal status."""

    message: OptionalNullable[str] = UNSET
    r"""Provides further details about failure indicated. This field is only populated if the`code` field is set to `error`."""

    @field_serializer("code")
    def serialize_code(self, value):
        if isinstance(value, str):
            try:
                return models.StatusReasonCodeResponse(value)
            except ValueError:
                return value
        return value

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["code", "message"])
        nullable_fields = set(["message"])
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k, serialized.get(n))
            is_nullable_and_explicitly_set = (
                k in nullable_fields
                and (self.__pydantic_fields_set__.intersection({n}))  # pylint: disable=no-member
            )

            if val != UNSET_SENTINEL:
                if (
                    val is not None
                    or k not in optional_fields
                    or is_nullable_and_explicitly_set
                ):
                    m[k] = val

        return m
