from http import HTTPStatus
from typing import Any

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.error_response import ErrorResponse
from ...models.http_validation_error import HTTPValidationError
from ...models.list_storage_connections_request import ListStorageConnectionsRequest
from ...models.storage_connection_list_response import StorageConnectionListResponse
from ...types import UNSET, Response, Unset


def _get_kwargs(
    *,
    body: ListStorageConnectionsRequest | Unset = UNSET,
    limit: int | None | Unset = UNSET,
    offset: int | None | Unset = UNSET,
    cursor: None | str | Unset = UNSET,
    include_total: bool | Unset = False,
    authorization: str | Unset = UNSET,
) -> dict[str, Any]:
    headers: dict[str, Any] = {}
    if not isinstance(authorization, Unset):
        headers["Authorization"] = authorization

    params: dict[str, Any] = {}

    json_limit: int | None | Unset
    if isinstance(limit, Unset):
        json_limit = UNSET
    else:
        json_limit = limit
    params["limit"] = json_limit

    json_offset: int | None | Unset
    if isinstance(offset, Unset):
        json_offset = UNSET
    else:
        json_offset = offset
    params["offset"] = json_offset

    json_cursor: None | str | Unset
    if isinstance(cursor, Unset):
        json_cursor = UNSET
    else:
        json_cursor = cursor
    params["cursor"] = json_cursor

    params["include_total"] = include_total

    params = {k: v for k, v in params.items() if v is not UNSET and v is not None}

    _kwargs: dict[str, Any] = {
        "method": "post",
        "url": "/v1/organizations/connections/list",
        "params": params,
    }

    if not isinstance(body, Unset):
        _kwargs["json"] = body.to_dict()

    headers["Content-Type"] = "application/json"

    _kwargs["headers"] = headers
    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> ErrorResponse | HTTPValidationError | StorageConnectionListResponse | None:
    if response.status_code == 200:
        response_200 = StorageConnectionListResponse.from_dict(response.json())

        return response_200

    if response.status_code == 400:
        response_400 = ErrorResponse.from_dict(response.json())

        return response_400

    if response.status_code == 401:
        response_401 = ErrorResponse.from_dict(response.json())

        return response_401

    if response.status_code == 403:
        response_403 = ErrorResponse.from_dict(response.json())

        return response_403

    if response.status_code == 404:
        response_404 = ErrorResponse.from_dict(response.json())

        return response_404

    if response.status_code == 422:
        response_422 = HTTPValidationError.from_dict(response.json())

        return response_422

    if response.status_code == 500:
        response_500 = ErrorResponse.from_dict(response.json())

        return response_500

    if client.raise_on_unexpected_status:
        raise errors.UnexpectedStatus(response.status_code, response.content)
    else:
        return None


def _build_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> Response[ErrorResponse | HTTPValidationError | StorageConnectionListResponse]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    *,
    client: AuthenticatedClient | Client,
    body: ListStorageConnectionsRequest | Unset = UNSET,
    limit: int | None | Unset = UNSET,
    offset: int | None | Unset = UNSET,
    cursor: None | str | Unset = UNSET,
    include_total: bool | Unset = False,
    authorization: str | Unset = UNSET,
) -> Response[ErrorResponse | HTTPValidationError | StorageConnectionListResponse]:
    r""" List Storage Connections

     List storage connections for the authenticated organization.

    Returns paginated results with optional filters for provider type, status,
    and active flag. Results are sorted by creation date (newest first).

    **Use Cases:**
    - List all active Google Drive connections
    - Find failed connections that need attention
    - Filter by provider type for sync configuration

    **Example:**
    ```bash
    curl -X POST \"http://localhost:8000/v1/organizations/connections/list\" \
      -H \"Authorization: Bearer YOUR_API_KEY\" \
      -H \"Content-Type: application/json\" \
      -d '{
        \"provider_type\": \"google_drive\",
        \"is_active\": true
      }'
    ```

    Args:
        limit (int | None | Unset):
        offset (int | None | Unset):
        cursor (None | str | Unset):
        include_total (bool | Unset):  Default: False.
        authorization (str | Unset): REQUIRED: Bearer token authentication using your API key.
            Format: 'Bearer sk_xxxxxxxxxxxxx'. You can create API keys in the Mixpeek dashboard under
            Organization Settings.
        body (ListStorageConnectionsRequest | Unset): Request payload for listing storage
            connections with filters.

            Use this to filter connections by provider type, status, or active flag.
            Results are paginated automatically.

            **Use Cases:**
            - List all active Google Drive connections
            - Find failed connections that need attention
            - Filter by provider type for sync configuration

            **Examples:**
            ```python
            # List all active Google Drive connections
            {
                "provider_type": "google_drive",
                "is_active": True
            }

            # Find failed connections
            {
                "status": "failed"
            }
            ```

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[ErrorResponse | HTTPValidationError | StorageConnectionListResponse]
     """

    kwargs = _get_kwargs(
        body=body,
        limit=limit,
        offset=offset,
        cursor=cursor,
        include_total=include_total,
        authorization=authorization,
    )

    response = client.get_httpx_client().request(
        **kwargs,
    )

    return _build_response(client=client, response=response)


def sync(
    *,
    client: AuthenticatedClient | Client,
    body: ListStorageConnectionsRequest | Unset = UNSET,
    limit: int | None | Unset = UNSET,
    offset: int | None | Unset = UNSET,
    cursor: None | str | Unset = UNSET,
    include_total: bool | Unset = False,
    authorization: str | Unset = UNSET,
) -> ErrorResponse | HTTPValidationError | StorageConnectionListResponse | None:
    r""" List Storage Connections

     List storage connections for the authenticated organization.

    Returns paginated results with optional filters for provider type, status,
    and active flag. Results are sorted by creation date (newest first).

    **Use Cases:**
    - List all active Google Drive connections
    - Find failed connections that need attention
    - Filter by provider type for sync configuration

    **Example:**
    ```bash
    curl -X POST \"http://localhost:8000/v1/organizations/connections/list\" \
      -H \"Authorization: Bearer YOUR_API_KEY\" \
      -H \"Content-Type: application/json\" \
      -d '{
        \"provider_type\": \"google_drive\",
        \"is_active\": true
      }'
    ```

    Args:
        limit (int | None | Unset):
        offset (int | None | Unset):
        cursor (None | str | Unset):
        include_total (bool | Unset):  Default: False.
        authorization (str | Unset): REQUIRED: Bearer token authentication using your API key.
            Format: 'Bearer sk_xxxxxxxxxxxxx'. You can create API keys in the Mixpeek dashboard under
            Organization Settings.
        body (ListStorageConnectionsRequest | Unset): Request payload for listing storage
            connections with filters.

            Use this to filter connections by provider type, status, or active flag.
            Results are paginated automatically.

            **Use Cases:**
            - List all active Google Drive connections
            - Find failed connections that need attention
            - Filter by provider type for sync configuration

            **Examples:**
            ```python
            # List all active Google Drive connections
            {
                "provider_type": "google_drive",
                "is_active": True
            }

            # Find failed connections
            {
                "status": "failed"
            }
            ```

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        ErrorResponse | HTTPValidationError | StorageConnectionListResponse
     """

    return sync_detailed(
        client=client,
        body=body,
        limit=limit,
        offset=offset,
        cursor=cursor,
        include_total=include_total,
        authorization=authorization,
    ).parsed


async def asyncio_detailed(
    *,
    client: AuthenticatedClient | Client,
    body: ListStorageConnectionsRequest | Unset = UNSET,
    limit: int | None | Unset = UNSET,
    offset: int | None | Unset = UNSET,
    cursor: None | str | Unset = UNSET,
    include_total: bool | Unset = False,
    authorization: str | Unset = UNSET,
) -> Response[ErrorResponse | HTTPValidationError | StorageConnectionListResponse]:
    r""" List Storage Connections

     List storage connections for the authenticated organization.

    Returns paginated results with optional filters for provider type, status,
    and active flag. Results are sorted by creation date (newest first).

    **Use Cases:**
    - List all active Google Drive connections
    - Find failed connections that need attention
    - Filter by provider type for sync configuration

    **Example:**
    ```bash
    curl -X POST \"http://localhost:8000/v1/organizations/connections/list\" \
      -H \"Authorization: Bearer YOUR_API_KEY\" \
      -H \"Content-Type: application/json\" \
      -d '{
        \"provider_type\": \"google_drive\",
        \"is_active\": true
      }'
    ```

    Args:
        limit (int | None | Unset):
        offset (int | None | Unset):
        cursor (None | str | Unset):
        include_total (bool | Unset):  Default: False.
        authorization (str | Unset): REQUIRED: Bearer token authentication using your API key.
            Format: 'Bearer sk_xxxxxxxxxxxxx'. You can create API keys in the Mixpeek dashboard under
            Organization Settings.
        body (ListStorageConnectionsRequest | Unset): Request payload for listing storage
            connections with filters.

            Use this to filter connections by provider type, status, or active flag.
            Results are paginated automatically.

            **Use Cases:**
            - List all active Google Drive connections
            - Find failed connections that need attention
            - Filter by provider type for sync configuration

            **Examples:**
            ```python
            # List all active Google Drive connections
            {
                "provider_type": "google_drive",
                "is_active": True
            }

            # Find failed connections
            {
                "status": "failed"
            }
            ```

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[ErrorResponse | HTTPValidationError | StorageConnectionListResponse]
     """

    kwargs = _get_kwargs(
        body=body,
        limit=limit,
        offset=offset,
        cursor=cursor,
        include_total=include_total,
        authorization=authorization,
    )

    response = await client.get_async_httpx_client().request(**kwargs)

    return _build_response(client=client, response=response)


async def asyncio(
    *,
    client: AuthenticatedClient | Client,
    body: ListStorageConnectionsRequest | Unset = UNSET,
    limit: int | None | Unset = UNSET,
    offset: int | None | Unset = UNSET,
    cursor: None | str | Unset = UNSET,
    include_total: bool | Unset = False,
    authorization: str | Unset = UNSET,
) -> ErrorResponse | HTTPValidationError | StorageConnectionListResponse | None:
    r""" List Storage Connections

     List storage connections for the authenticated organization.

    Returns paginated results with optional filters for provider type, status,
    and active flag. Results are sorted by creation date (newest first).

    **Use Cases:**
    - List all active Google Drive connections
    - Find failed connections that need attention
    - Filter by provider type for sync configuration

    **Example:**
    ```bash
    curl -X POST \"http://localhost:8000/v1/organizations/connections/list\" \
      -H \"Authorization: Bearer YOUR_API_KEY\" \
      -H \"Content-Type: application/json\" \
      -d '{
        \"provider_type\": \"google_drive\",
        \"is_active\": true
      }'
    ```

    Args:
        limit (int | None | Unset):
        offset (int | None | Unset):
        cursor (None | str | Unset):
        include_total (bool | Unset):  Default: False.
        authorization (str | Unset): REQUIRED: Bearer token authentication using your API key.
            Format: 'Bearer sk_xxxxxxxxxxxxx'. You can create API keys in the Mixpeek dashboard under
            Organization Settings.
        body (ListStorageConnectionsRequest | Unset): Request payload for listing storage
            connections with filters.

            Use this to filter connections by provider type, status, or active flag.
            Results are paginated automatically.

            **Use Cases:**
            - List all active Google Drive connections
            - Find failed connections that need attention
            - Filter by provider type for sync configuration

            **Examples:**
            ```python
            # List all active Google Drive connections
            {
                "provider_type": "google_drive",
                "is_active": True
            }

            # Find failed connections
            {
                "status": "failed"
            }
            ```

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        ErrorResponse | HTTPValidationError | StorageConnectionListResponse
     """

    return (
        await asyncio_detailed(
            client=client,
            body=body,
            limit=limit,
            offset=offset,
            cursor=cursor,
            include_total=include_total,
            authorization=authorization,
        )
    ).parsed
