# 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, Literal, cast

from workos._types import _format_datetime, _parse_datetime, _raise_deserialize_error

from .connect_application_oauth_redirect_uris import ConnectApplicationOAuthRedirectUris


@dataclass(slots=True)
class ConnectApplicationOAuth:
    """Connect Application O Auth model."""

    object: Literal["connect_application"]
    """Distinguishes the connect application object."""
    id: str
    """The unique ID of the connect application."""
    client_id: str
    """The client ID of the connect application."""
    description: str | None
    """A description of the connect application."""
    name: str
    """The name of the connect application."""
    scopes: list[str]
    """The scopes available for this application."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""
    application_type: Literal["oauth"]
    """The type of the application."""
    redirect_uris: list[ConnectApplicationOAuthRedirectUris]
    """The redirect URIs configured for this application."""
    uses_pkce: bool
    """Whether the application uses PKCE for authorization."""
    is_first_party: bool
    """Whether the application is a first-party application."""
    was_dynamically_registered: bool | None = None
    """Whether the application was dynamically registered."""
    organization_id: str | None = None
    """The ID of the organization the application belongs to."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> ConnectApplicationOAuth:
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "connect_application"),
                id=data["id"],
                client_id=data["client_id"],
                description=data["description"],
                name=data["name"],
                scopes=data["scopes"],
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
                application_type=data.get("application_type", "oauth"),
                redirect_uris=[
                    ConnectApplicationOAuthRedirectUris.from_dict(
                        cast(dict[str, Any], item)
                    )
                    for item in cast(list[Any], data["redirect_uris"])
                ],
                uses_pkce=data["uses_pkce"],
                is_first_party=data["is_first_party"],
                was_dynamically_registered=data.get("was_dynamically_registered"),
                organization_id=data.get("organization_id"),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("ConnectApplicationOAuth", 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["client_id"] = self.client_id
        if self.description is not None:
            result["description"] = self.description
        else:
            result["description"] = None
        result["name"] = self.name
        result["scopes"] = self.scopes
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        result["application_type"] = self.application_type
        result["redirect_uris"] = [item.to_dict() for item in self.redirect_uris]
        result["uses_pkce"] = self.uses_pkce
        result["is_first_party"] = self.is_first_party
        if self.was_dynamically_registered is not None:
            result["was_dynamically_registered"] = self.was_dynamically_registered
        if self.organization_id is not None:
            result["organization_id"] = self.organization_id
        return result
