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

from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime
from enum import Enum
from typing import Any, Literal, cast

from workos._types import _format_datetime, _parse_datetime, _raise_deserialize_error
from workos.common.models.data_integration_auth_methods import (
    DataIntegrationAuthMethods,
)
from workos.common.models.data_integration_state import DataIntegrationState

from .data_integration_credential import DataIntegrationCredential
from .data_integration_custom_provider import DataIntegrationCustomProvider
from .data_integration_installation import DataIntegrationInstallation


@dataclass(slots=True)
class DataIntegration:
    """Data Integration model."""

    object: Literal["data_integration"]
    """Distinguishes the Data Integration object."""
    id: str
    """Unique identifier of the Data Integration."""
    slug: str
    """The provider slug for this Data Integration."""
    integration_type: str
    """The integration type derived from the provider."""
    description: str | None
    """An optional description of the Data Integration."""
    enabled: bool
    """Whether the Data Integration is enabled."""
    state: DataIntegrationState
    """The state of the Data Integration."""
    scopes: list[str] | None
    """The OAuth scopes configured for the Data Integration. `null` when the provider's configured scopes are used."""
    redirect_uri: str
    """The OAuth redirect URI to register with the provider when configuring the custom application."""
    auth_methods: list[DataIntegrationAuthMethods]
    """How accounts authenticate with the provider for this Data Integration."""
    credentials: DataIntegrationCredential
    """The credentials configured for the Data Integration."""
    installation: DataIntegrationInstallation | None
    """The tenant installation created when an API key was supplied at creation time; `null` otherwise. Not populated on list/get responses."""
    config: dict[str, str]
    """Provider-specific config values set on the Data Integration (e.g. a Snowflake `account_identifier`), keyed by config field. Only fields the provider declares are accepted."""
    custom_provider: DataIntegrationCustomProvider | None
    """The OAuth definition when this is a custom provider; `null` for built-in providers."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> DataIntegration:
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "data_integration"),
                id=data["id"],
                slug=data["slug"],
                integration_type=data["integration_type"],
                description=data["description"],
                enabled=data["enabled"],
                state=DataIntegrationState(data["state"]),
                scopes=data["scopes"],
                redirect_uri=data["redirect_uri"],
                auth_methods=[
                    DataIntegrationAuthMethods(item)
                    for item in cast(list[Any], data["auth_methods"])
                ],
                credentials=DataIntegrationCredential.from_dict(
                    cast(dict[str, Any], data["credentials"])
                ),
                installation=DataIntegrationInstallation.from_dict(
                    cast(dict[str, Any], _v_installation)
                )
                if (_v_installation := data["installation"]) is not None
                else None,
                config=data["config"],
                custom_provider=DataIntegrationCustomProvider.from_dict(
                    cast(dict[str, Any], _v_custom_provider)
                )
                if (_v_custom_provider := data["custom_provider"]) is not None
                else None,
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("DataIntegration", e)

    def to_dict(self) -> dict[str, Any]:
        """Serialize to a dictionary."""
        result: dict[str, Any] = {}
        result["object"] = self.object
        result["id"] = self.id
        result["slug"] = self.slug
        result["integration_type"] = self.integration_type
        if self.description is not None:
            result["description"] = self.description
        else:
            result["description"] = None
        result["enabled"] = self.enabled
        result["state"] = (
            self.state.value if isinstance(self.state, Enum) else self.state
        )
        if self.scopes is not None:
            result["scopes"] = self.scopes
        else:
            result["scopes"] = None
        result["redirect_uri"] = self.redirect_uri
        result["auth_methods"] = [
            item.value if isinstance(item, Enum) else item for item in self.auth_methods
        ]
        result["credentials"] = self.credentials.to_dict()
        if self.installation is not None:
            result["installation"] = self.installation.to_dict()
        else:
            result["installation"] = None
        result["config"] = self.config
        if self.custom_provider is not None:
            result["custom_provider"] = self.custom_provider.to_dict()
        else:
            result["custom_provider"] = None
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        return result
