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

from __future__ import annotations

from dataclasses import dataclass
from enum import Enum
from typing import Any

from workos._types import _raise_deserialize_error
from workos.common.models.custom_provider_definition_authenticate_via import (
    CustomProviderDefinitionAuthenticateVia,
)


@dataclass(slots=True)
class CustomProviderDefinition:
    """Custom Provider Definition model."""

    name: str
    """A descriptive name for the custom provider."""
    authorization_url: str
    """The provider's OAuth authorization endpoint."""
    token_url: str
    """The provider's OAuth token endpoint."""
    refresh_token_url: str | None = None
    """The endpoint used to refresh tokens, if different from the token endpoint."""
    pkce_enabled: bool | None = None
    """Whether PKCE is used during the authorization code flow. Defaults to `true`."""
    request_scope_separator: str | None = None
    """The separator used to join requested scopes. Defaults to a space."""
    scopes_required: bool | None = None
    """Whether at least one scope must be selected when connecting an account. Defaults to `false`."""
    client_secret_required: bool | None = None
    """Whether a client secret is required for this provider. Defaults to `true`."""
    additional_authorization_parameters: dict[str, str] | None = None
    """Additional static query parameters appended to the authorization request."""
    token_body_content_type: str | None = None
    """The Content-Type used when exchanging the token request."""
    authenticate_via: CustomProviderDefinitionAuthenticateVia | None = None
    """How client credentials are sent when exchanging authorization codes and refreshing tokens."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> CustomProviderDefinition:
        """Deserialize from a dictionary."""
        try:
            return cls(
                name=data["name"],
                authorization_url=data["authorization_url"],
                token_url=data["token_url"],
                refresh_token_url=data.get("refresh_token_url"),
                pkce_enabled=data.get("pkce_enabled"),
                request_scope_separator=data.get("request_scope_separator"),
                scopes_required=data.get("scopes_required"),
                client_secret_required=data.get("client_secret_required"),
                additional_authorization_parameters=data.get(
                    "additional_authorization_parameters"
                ),
                token_body_content_type=data.get("token_body_content_type"),
                authenticate_via=CustomProviderDefinitionAuthenticateVia(
                    _v_authenticate_via
                )
                if (_v_authenticate_via := data.get("authenticate_via")) is not None
                else None,
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("CustomProviderDefinition", e)

    def to_dict(self) -> dict[str, Any]:
        """Serialize to a dictionary."""
        result: dict[str, Any] = {}
        result["name"] = self.name
        result["authorization_url"] = self.authorization_url
        result["token_url"] = self.token_url
        if self.refresh_token_url is not None:
            result["refresh_token_url"] = self.refresh_token_url
        else:
            result["refresh_token_url"] = None
        if self.pkce_enabled is not None:
            result["pkce_enabled"] = self.pkce_enabled
        if self.request_scope_separator is not None:
            result["request_scope_separator"] = self.request_scope_separator
        if self.scopes_required is not None:
            result["scopes_required"] = self.scopes_required
        if self.client_secret_required is not None:
            result["client_secret_required"] = self.client_secret_required
        if self.additional_authorization_parameters is not None:
            result["additional_authorization_parameters"] = (
                self.additional_authorization_parameters
            )
        if self.token_body_content_type is not None:
            result["token_body_content_type"] = self.token_body_content_type
        if self.authenticate_via is not None:
            result["authenticate_via"] = (
                self.authenticate_via.value
                if isinstance(self.authenticate_via, Enum)
                else self.authenticate_via
            )
        return result
