from dynamix_sdk import base, config
from dynamix_sdk.utils import F_DECORATOR


class DECS3OAuth(config.AuthenticatorProtocol):
    _config: config.Config
    client_id: str
    client_secret: str

    def __init__(
        self,
        url: str,
        client_id: str,
        client_secret: str,
        verify_ssl: bool = True,
        http503_attempts: int = 10,
        http503_attempts_interval: int = 5,
        result_extra_allow: bool = True,
        wrap_request_exceptions: bool = False,
        f_decorators: list[F_DECORATOR] | None = None,
    ):
        self._config = config.Config(
            base_api_path='/v1',
            url=url,
            verify_ssl=verify_ssl,
            http503_attempts=http503_attempts,
            http503_attempts_interval=http503_attempts_interval,
            result_extra_allow=result_extra_allow,
            wrap_request_exceptions=wrap_request_exceptions,
            f_decorators=f_decorators,
        )

        self.client_id = client_id
        self.client_secret = client_secret

    @property
    def api(self):
        return DECS3OAPI(config=self._config)

    def get_jwt(self):
        return self.api.oauth__access_token(
            client_id=self.client_id,
            client_secret=self.client_secret,
            grant_type='client_credentials',
            response_type='id_token',
        )


class DECS3OOauthAccesstokenResultStr(base.BaseAPIResultStr):
    pass


class DECS3OOauthAccesstokenProtocol(base.BasePostAPIFunctionProtocol):
    def oauth__access_token(
        self,
        *,
        client_id: str,
        client_secret: str,
        grant_type: None | str = None,
        response_type: None | str = None,
        validity: None | int = None,
    ) -> DECS3OOauthAccesstokenResultStr:
        ...


class DECS3OAPI(
    base.BaseAPI,
    DECS3OOauthAccesstokenProtocol,
    path_mapping_dict={'oauth__access_token': 'oauth/access_token'},
    name_mapping_dict={
        'client_id': 'client_id',
        'client_secret': 'client_secret',
        'grant_type': 'grant_type',
        'response_type': 'response_type',
        'validity': 'validity',
    },
    post_json=False,
):
    pass
