"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from factify.types import BaseModel, UNSET_SENTINEL
from pydantic import model_serializer
from typing import Literal, Optional
from typing_extensions import NotRequired, TypedDict


Reason = Literal[
    "missing_required_field",
    "invalid_field_value",
    "invalid_enum_value",
    "invalid_cursor",
    "invalid_id_format",
    "missing_api_key",
    "invalid_api_key",
    "expired_api_key",
    "invalid_token",
    "expired_token",
    "authentication_required",
    "insufficient_permissions",
    "resource_not_found",
    "resource_already_exists",
    "rate_limit_exceeded",
    "quota_exceeded",
    "precondition_failed",
    "feature_disabled",
    "internal_error",
    "service_unavailable",
    "unimplemented",
]
r"""Machine-readable error reason for programmatic handling"""


class ErrorDetailTypedDict(TypedDict):
    param: NotRequired[str]
    r"""The parameter that caused the error (only set for validation errors)"""
    reason: NotRequired[Reason]
    r"""Machine-readable error reason for programmatic handling"""
    request_id: NotRequired[str]
    r"""Request correlation ID for tracing and log lookup (e.g., \"req_01arwx4k8xrgqskvxq69gdn019\")"""


class ErrorDetail(BaseModel):
    param: Optional[str] = None
    r"""The parameter that caused the error (only set for validation errors)"""

    reason: Optional[Reason] = None
    r"""Machine-readable error reason for programmatic handling"""

    request_id: Optional[str] = None
    r"""Request correlation ID for tracing and log lookup (e.g., \"req_01arwx4k8xrgqskvxq69gdn019\")"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["param", "reason", "request_id"])
        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))

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

        return m
