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

from __future__ import annotations

from dataclasses import dataclass
from typing import cast
from typing import Any, Dict, List, Literal, Optional
from workos._types import _raise_deserialize_error

from .redirect_uri_input import RedirectUriInput


@dataclass(slots=True)
class CreateOAuthApplication:
    """Create O Auth Application model."""

    name: str
    """The name of the application."""
    application_type: Literal["oauth"]
    """The type of application to create."""
    is_first_party: bool
    """Whether this is a first-party application. Third-party applications require an organization_id."""
    description: Optional[str] = None
    """A description for the application."""
    scopes: Optional[List[str]] = None
    """The OAuth scopes granted to the application."""
    redirect_uris: Optional[List["RedirectUriInput"]] = None
    """Redirect URIs for the application."""
    uses_pkce: Optional[bool] = None
    """Whether the application uses PKCE (Proof Key for Code Exchange)."""
    organization_id: Optional[str] = None
    """The organization ID this application belongs to. Required when is_first_party is false."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "CreateOAuthApplication":
        """Deserialize from a dictionary."""
        try:
            return cls(
                name=data["name"],
                application_type=data.get("application_type", "oauth"),
                is_first_party=data["is_first_party"],
                description=data.get("description"),
                scopes=data.get("scopes"),
                redirect_uris=[
                    RedirectUriInput.from_dict(cast(Dict[str, Any], item))
                    for item in cast(list[Any], _v_redirect_uris)
                ]
                if (_v_redirect_uris := data.get("redirect_uris")) is not None
                else None,
                uses_pkce=data.get("uses_pkce"),
                organization_id=data.get("organization_id"),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("CreateOAuthApplication", e)

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to a dictionary."""
        result: Dict[str, Any] = {}
        result["name"] = self.name
        result["application_type"] = self.application_type
        result["is_first_party"] = self.is_first_party
        if self.description is not None:
            result["description"] = self.description
        else:
            result["description"] = None
        if self.scopes is not None:
            result["scopes"] = self.scopes
        else:
            result["scopes"] = None
        if self.redirect_uris is not None:
            result["redirect_uris"] = [item.to_dict() for item in self.redirect_uris]
        else:
            result["redirect_uris"] = None
        if self.uses_pkce is not None:
            result["uses_pkce"] = self.uses_pkce
        else:
            result["uses_pkce"] = None
        if self.organization_id is not None:
            result["organization_id"] = self.organization_id
        else:
            result["organization_id"] = None
        return result
