# This file is auto-generated by oagen. Do not edit.

from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime
from typing import Any, Dict, Literal, Optional
from workos._types import _raise_deserialize_error
from workos._types import _format_datetime, _parse_datetime


@dataclass(slots=True)
class PipesConnectedAccountConnectionFailedData:
    """The event payload."""

    object: Literal["connection_failed"]
    """The object type."""
    data_integration_id: str
    """The unique ID of the data integration."""
    provider_slug: str
    """The provider slug for this connection attempt."""
    user_id: Optional[str]
    """The ID of the User the connection attempt belongs to."""
    organization_id: Optional[str]
    """The ID of the Organization the connection attempt belongs to."""
    error_code: str
    """A machine-readable error code for the failure."""
    error_reason: Optional[str]
    """A human-readable explanation of the failure."""
    provider_error: Optional[str]
    """The raw error code returned by the OAuth provider."""
    provider_error_description: Optional[str]
    """The raw error description returned by the OAuth provider."""
    created_at: datetime
    """An ISO 8601 timestamp."""

    @classmethod
    def from_dict(
        cls, data: Dict[str, Any]
    ) -> "PipesConnectedAccountConnectionFailedData":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "connection_failed"),
                data_integration_id=data["data_integration_id"],
                provider_slug=data["provider_slug"],
                user_id=data["user_id"],
                organization_id=data["organization_id"],
                error_code=data["error_code"],
                error_reason=data["error_reason"],
                provider_error=data["provider_error"],
                provider_error_description=data["provider_error_description"],
                created_at=_parse_datetime(data["created_at"]),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("PipesConnectedAccountConnectionFailedData", e)

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to a dictionary."""
        result: Dict[str, Any] = {}
        result["object"] = self.object
        result["data_integration_id"] = self.data_integration_id
        result["provider_slug"] = self.provider_slug
        if self.user_id is not None:
            result["user_id"] = self.user_id
        else:
            result["user_id"] = None
        if self.organization_id is not None:
            result["organization_id"] = self.organization_id
        else:
            result["organization_id"] = None
        result["error_code"] = self.error_code
        if self.error_reason is not None:
            result["error_reason"] = self.error_reason
        else:
            result["error_reason"] = None
        if self.provider_error is not None:
            result["provider_error"] = self.provider_error
        else:
            result["provider_error"] = None
        if self.provider_error_description is not None:
            result["provider_error_description"] = self.provider_error_description
        else:
            result["provider_error_description"] = None
        result["created_at"] = _format_datetime(self.created_at)
        return result
