# This file was auto-generated by Fern from our API Definition.

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.o_auth_initiate_response import OAuthInitiateResponse
from ..types.o_auth_token_response import OAuthTokenResponse
from .raw_client import AsyncRawOAuthClient, RawOAuthClient


class OAuthClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawOAuthClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawOAuthClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawOAuthClient
        """
        return self._raw_client

    def initiate_oauth(
        self,
        *,
        redirect_uri: str,
        code_challenge: typing.Optional[str] = None,
        connection_id: typing.Optional[str] = None,
        organization_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> OAuthInitiateResponse:
        """
        Generate authorization URL for OAuth authentication.

        Parameters
        ----------
        redirect_uri : str
            URI to redirect after authentication

        code_challenge : typing.Optional[str]
            PKCE code challenge (S256)

        connection_id : typing.Optional[str]
            Optional WorkOS connection ID for SSO

        organization_id : typing.Optional[str]
            Organization ID to scope the authentication to

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        OAuthInitiateResponse
            Successful Response

        Examples
        --------
        from terminaluse import TerminalUseBase

        client = TerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        client.o_auth.initiate_oauth(
            redirect_uri="redirect_uri",
        )
        """
        _response = self._raw_client.initiate_oauth(
            redirect_uri=redirect_uri,
            code_challenge=code_challenge,
            connection_id=connection_id,
            organization_id=organization_id,
            request_options=request_options,
        )
        return _response.data

    def exchange_token(
        self, *, code: str, request_options: typing.Optional[RequestOptions] = None
    ) -> OAuthTokenResponse:
        """
        Exchange authorization code for access token.

        Parameters
        ----------
        code : str
            Authorization code from OAuth callback

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        OAuthTokenResponse
            Successful Response

        Examples
        --------
        from terminaluse import TerminalUseBase

        client = TerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        client.o_auth.exchange_token(
            code="code",
        )
        """
        _response = self._raw_client.exchange_token(code=code, request_options=request_options)
        return _response.data


class AsyncOAuthClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawOAuthClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawOAuthClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawOAuthClient
        """
        return self._raw_client

    async def initiate_oauth(
        self,
        *,
        redirect_uri: str,
        code_challenge: typing.Optional[str] = None,
        connection_id: typing.Optional[str] = None,
        organization_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> OAuthInitiateResponse:
        """
        Generate authorization URL for OAuth authentication.

        Parameters
        ----------
        redirect_uri : str
            URI to redirect after authentication

        code_challenge : typing.Optional[str]
            PKCE code challenge (S256)

        connection_id : typing.Optional[str]
            Optional WorkOS connection ID for SSO

        organization_id : typing.Optional[str]
            Organization ID to scope the authentication to

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        OAuthInitiateResponse
            Successful Response

        Examples
        --------
        import asyncio

        from terminaluse import AsyncTerminalUseBase

        client = AsyncTerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.o_auth.initiate_oauth(
                redirect_uri="redirect_uri",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.initiate_oauth(
            redirect_uri=redirect_uri,
            code_challenge=code_challenge,
            connection_id=connection_id,
            organization_id=organization_id,
            request_options=request_options,
        )
        return _response.data

    async def exchange_token(
        self, *, code: str, request_options: typing.Optional[RequestOptions] = None
    ) -> OAuthTokenResponse:
        """
        Exchange authorization code for access token.

        Parameters
        ----------
        code : str
            Authorization code from OAuth callback

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        OAuthTokenResponse
            Successful Response

        Examples
        --------
        import asyncio

        from terminaluse import AsyncTerminalUseBase

        client = AsyncTerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.o_auth.exchange_token(
                code="code",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.exchange_token(code=code, request_options=request_options)
        return _response.data
