from typing import Any, Dict, Optional, Union
from urllib.parse import urlencode

from xima_ai._vendor.openai import NOT_GIVEN, NotGiven
from xima_ai.api_resources.apis.api_resource import APIResource, AsyncAPIResource
from xima_ai.api_resources.base_client import APIClient, AsyncAPIClient
from xima_ai.api_resources.types.providers_type import (
    ProviderCreateResponse,
    ProviderDetailResponse,
    ProviderListResponse,
    ProviderUpdateResponse,
)
from xima_ai.api_resources.utils import GenericResponse, XimaAiApiPaths


class Providers(APIResource):
    def __init__(self, client: APIClient) -> None:
        super().__init__(client)

    def create(
        self,
        *,
        name: str,
        integration_id: str,
        workspace_id: Union[str, NotGiven] = NOT_GIVEN,
        slug: Union[str, NotGiven] = NOT_GIVEN,
        note: Union[str, NotGiven] = NOT_GIVEN,
        usage_limits: Union[Dict[str, Any], NotGiven] = NOT_GIVEN,
        rate_limits: Union[Dict[str, Any], NotGiven] = NOT_GIVEN,
        expires_at: Union[str, NotGiven] = NOT_GIVEN,
        **kwargs: Any,
    ) -> ProviderCreateResponse:
        body = {
            "name": name,
            "integration_id": integration_id,
            "workspace_id": workspace_id,
            "slug": slug,
            "note": note,
            "usage_limits": usage_limits,
            "rate_limits": rate_limits,
            "expires_at": expires_at,
            **kwargs,
        }
        return self._post(
            f"{XimaAiApiPaths.PROVIDERS_API}",
            body=body,
            params=None,
            cast_to=ProviderCreateResponse,
            stream=False,
            stream_cls=None,
            headers={},
        )

    def list(
        self,
        *,
        current_page: Optional[int] = 0,
        page_size: Optional[int] = 50,
        workspace_id: Union[str, NotGiven] = NOT_GIVEN,
    ) -> ProviderListResponse:
        query = {
            "current_page": current_page,
            "page_size": page_size,
            "workspace_id": workspace_id,
        }
        filtered_query = {k: v for k, v in query.items() if v is not NOT_GIVEN}
        query_string = urlencode(filtered_query)
        return self._get(
            f"{XimaAiApiPaths.PROVIDERS_API}?{query_string}",
            params=None,
            body=None,
            cast_to=ProviderListResponse,
            stream=False,
            stream_cls=None,
            headers={},
        )

    def retrieve(
        self,
        *,
        slug: str,
        workspace_id: Union[str, NotGiven] = NOT_GIVEN,
    ) -> ProviderDetailResponse:
        query = {
            "workspace_id": workspace_id,
        }
        filtered_query = {k: v for k, v in query.items() if v is not NOT_GIVEN}
        query_string = urlencode(filtered_query)
        path = f"{XimaAiApiPaths.PROVIDERS_API}/{slug}"
        if query_string:
            path = f"{path}?{query_string}"
        return self._get(
            path,
            params=None,
            body=None,
            cast_to=ProviderDetailResponse,
            stream=False,
            stream_cls=None,
            headers={},
        )

    def update(
        self,
        *,
        slug: str,
        workspace_id: Union[str, NotGiven] = NOT_GIVEN,
        name: Union[str, NotGiven] = NOT_GIVEN,
        note: Union[str, NotGiven] = NOT_GIVEN,
        usage_limits: Union[Dict[str, Any], NotGiven] = NOT_GIVEN,
        rate_limits: Union[Dict[str, Any], NotGiven] = NOT_GIVEN,
        expires_at: Union[str, NotGiven] = NOT_GIVEN,
        reset_usage: Union[bool, NotGiven] = NOT_GIVEN,
        **kwargs: Any,
    ) -> ProviderUpdateResponse:
        body = {
            "name": name,
            "note": note,
            "usage_limits": usage_limits,
            "rate_limits": rate_limits,
            "expires_at": expires_at,
            "reset_usage": reset_usage,
            **kwargs,
        }
        query = {
            "workspace_id": workspace_id,
        }
        filtered_query = {k: v for k, v in query.items() if v is not NOT_GIVEN}
        query_string = urlencode(filtered_query)
        path = f"{XimaAiApiPaths.PROVIDERS_API}/{slug}"
        if query_string:
            path = f"{path}?{query_string}"
        return self._put(
            path,
            body=body,
            params=None,
            cast_to=ProviderUpdateResponse,
            stream=False,
            stream_cls=None,
            headers={},
        )

    def delete(
        self,
        *,
        slug: str,
        workspace_id: Union[str, NotGiven] = NOT_GIVEN,
    ) -> GenericResponse:
        query = {
            "workspace_id": workspace_id,
        }
        filtered_query = {k: v for k, v in query.items() if v is not NOT_GIVEN}
        query_string = urlencode(filtered_query)
        path = f"{XimaAiApiPaths.PROVIDERS_API}/{slug}"
        if query_string:
            path = f"{path}?{query_string}"
        return self._delete(
            path,
            params=None,
            body=None,
            cast_to=GenericResponse,
            stream=False,
            stream_cls=None,
            headers={},
        )


class AsyncProviders(AsyncAPIResource):
    def __init__(self, client: AsyncAPIClient) -> None:
        super().__init__(client)

    async def create(
        self,
        *,
        name: str,
        integration_id: str,
        workspace_id: Union[str, NotGiven] = NOT_GIVEN,
        slug: Union[str, NotGiven] = NOT_GIVEN,
        note: Union[str, NotGiven] = NOT_GIVEN,
        usage_limits: Union[Dict[str, Any], NotGiven] = NOT_GIVEN,
        rate_limits: Union[Dict[str, Any], NotGiven] = NOT_GIVEN,
        expires_at: Union[str, NotGiven] = NOT_GIVEN,
        **kwargs: Any,
    ) -> ProviderCreateResponse:
        body = {
            "name": name,
            "integration_id": integration_id,
            "workspace_id": workspace_id,
            "slug": slug,
            "note": note,
            "usage_limits": usage_limits,
            "rate_limits": rate_limits,
            "expires_at": expires_at,
            **kwargs,
        }
        return await self._post(
            f"{XimaAiApiPaths.PROVIDERS_API}",
            body=body,
            params=None,
            cast_to=ProviderCreateResponse,
            stream=False,
            stream_cls=None,
            headers={},
        )

    async def list(
        self,
        *,
        current_page: Optional[int] = 0,
        page_size: Optional[int] = 50,
        workspace_id: Union[str, NotGiven] = NOT_GIVEN,
    ) -> ProviderListResponse:
        query = {
            "current_page": current_page,
            "page_size": page_size,
            "workspace_id": workspace_id,
        }
        filtered_query = {k: v for k, v in query.items() if v is not NOT_GIVEN}
        query_string = urlencode(filtered_query)
        return await self._get(
            f"{XimaAiApiPaths.PROVIDERS_API}?{query_string}",
            params=None,
            body=None,
            cast_to=ProviderListResponse,
            stream=False,
            stream_cls=None,
            headers={},
        )

    async def retrieve(
        self,
        *,
        slug: str,
        workspace_id: Union[str, NotGiven] = NOT_GIVEN,
    ) -> ProviderDetailResponse:
        query = {
            "workspace_id": workspace_id,
        }
        filtered_query = {k: v for k, v in query.items() if v is not NOT_GIVEN}
        query_string = urlencode(filtered_query)
        path = f"{XimaAiApiPaths.PROVIDERS_API}/{slug}"
        if query_string:
            path = f"{path}?{query_string}"
        return await self._get(
            path,
            params=None,
            body=None,
            cast_to=ProviderDetailResponse,
            stream=False,
            stream_cls=None,
            headers={},
        )

    async def update(
        self,
        *,
        slug: str,
        workspace_id: Union[str, NotGiven] = NOT_GIVEN,
        name: Union[str, NotGiven] = NOT_GIVEN,
        note: Union[str, NotGiven] = NOT_GIVEN,
        usage_limits: Union[Dict[str, Any], NotGiven] = NOT_GIVEN,
        rate_limits: Union[Dict[str, Any], NotGiven] = NOT_GIVEN,
        expires_at: Union[str, NotGiven] = NOT_GIVEN,
        reset_usage: Union[bool, NotGiven] = NOT_GIVEN,
        **kwargs: Any,
    ) -> ProviderUpdateResponse:
        body = {
            "name": name,
            "note": note,
            "usage_limits": usage_limits,
            "rate_limits": rate_limits,
            "expires_at": expires_at,
            "reset_usage": reset_usage,
            **kwargs,
        }
        query = {
            "workspace_id": workspace_id,
        }
        filtered_query = {k: v for k, v in query.items() if v is not NOT_GIVEN}
        query_string = urlencode(filtered_query)
        path = f"{XimaAiApiPaths.PROVIDERS_API}/{slug}"
        if query_string:
            path = f"{path}?{query_string}"
        return await self._put(
            path,
            body=body,
            params=None,
            cast_to=ProviderUpdateResponse,
            stream=False,
            stream_cls=None,
            headers={},
        )

    async def delete(
        self,
        *,
        slug: str,
        workspace_id: Union[str, NotGiven] = NOT_GIVEN,
    ) -> GenericResponse:
        query = {
            "workspace_id": workspace_id,
        }
        filtered_query = {k: v for k, v in query.items() if v is not NOT_GIVEN}
        query_string = urlencode(filtered_query)
        path = f"{XimaAiApiPaths.PROVIDERS_API}/{slug}"
        if query_string:
            path = f"{path}?{query_string}"
        return await self._delete(
            path,
            params=None,
            body=None,
            cast_to=GenericResponse,
            stream=False,
            stream_cls=None,
            headers={},
        )
