from http import HTTPStatus
from typing import Any

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.stack_list_dir import StackListDir
from ...models.stack_list_response_200 import StackListResponse200
from ...types import UNSET, Response, Unset


def _get_kwargs(
    *,
    stack_id: str | Unset = UNSET,
    result_offset: float | Unset = 0.0,
    result_limit: float | Unset = 100.0,
    sort: str | Unset = UNSET,
    dir_: StackListDir | Unset = UNSET,
    recursive: bool | Unset = UNSET,
    ignore_duplicates: bool | Unset = UNSET,
    minimal: bool | Unset = UNSET,
    fields: str | Unset = UNSET,
) -> dict[str, Any]:

    params: dict[str, Any] = {}

    params["stackId"] = stack_id

    params["resultOffset"] = result_offset

    params["resultLimit"] = result_limit

    params["sort"] = sort

    json_dir_: int | Unset = UNSET
    if not isinstance(dir_, Unset):
        json_dir_ = dir_.value

    params["dir"] = json_dir_

    params["recursive"] = recursive

    params["ignoreDuplicates"] = ignore_duplicates

    params["minimal"] = minimal

    params["fields"] = fields

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

    _kwargs: dict[str, Any] = {
        "method": "get",
        "url": "/stack/list",
        "params": params,
    }

    return _kwargs


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

        return response_200

    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[StackListResponse200]:
    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,
    stack_id: str | Unset = UNSET,
    result_offset: float | Unset = 0.0,
    result_limit: float | Unset = 100.0,
    sort: str | Unset = UNSET,
    dir_: StackListDir | Unset = UNSET,
    recursive: bool | Unset = UNSET,
    ignore_duplicates: bool | Unset = UNSET,
    minimal: bool | Unset = UNSET,
    fields: str | Unset = UNSET,
) -> Response[StackListResponse200]:
    """List Documents in Stack

     Get a list of documents in a stack with pagination and filtering

    Args:
        stack_id (str | Unset): MongoDB ObjectId
        result_offset (float | Unset):  Default: 0.0.
        result_limit (float | Unset):  Default: 100.0.
        sort (str | Unset):
        dir_ (StackListDir | Unset):
        recursive (bool | Unset):
        ignore_duplicates (bool | Unset):
        minimal (bool | Unset):
        fields (str | Unset):

    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[StackListResponse200]
    """

    kwargs = _get_kwargs(
        stack_id=stack_id,
        result_offset=result_offset,
        result_limit=result_limit,
        sort=sort,
        dir_=dir_,
        recursive=recursive,
        ignore_duplicates=ignore_duplicates,
        minimal=minimal,
        fields=fields,
    )

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

    return _build_response(client=client, response=response)


def sync(
    *,
    client: AuthenticatedClient,
    stack_id: str | Unset = UNSET,
    result_offset: float | Unset = 0.0,
    result_limit: float | Unset = 100.0,
    sort: str | Unset = UNSET,
    dir_: StackListDir | Unset = UNSET,
    recursive: bool | Unset = UNSET,
    ignore_duplicates: bool | Unset = UNSET,
    minimal: bool | Unset = UNSET,
    fields: str | Unset = UNSET,
) -> StackListResponse200 | None:
    """List Documents in Stack

     Get a list of documents in a stack with pagination and filtering

    Args:
        stack_id (str | Unset): MongoDB ObjectId
        result_offset (float | Unset):  Default: 0.0.
        result_limit (float | Unset):  Default: 100.0.
        sort (str | Unset):
        dir_ (StackListDir | Unset):
        recursive (bool | Unset):
        ignore_duplicates (bool | Unset):
        minimal (bool | Unset):
        fields (str | Unset):

    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:
        StackListResponse200
    """

    return sync_detailed(
        client=client,
        stack_id=stack_id,
        result_offset=result_offset,
        result_limit=result_limit,
        sort=sort,
        dir_=dir_,
        recursive=recursive,
        ignore_duplicates=ignore_duplicates,
        minimal=minimal,
        fields=fields,
    ).parsed


async def asyncio_detailed(
    *,
    client: AuthenticatedClient,
    stack_id: str | Unset = UNSET,
    result_offset: float | Unset = 0.0,
    result_limit: float | Unset = 100.0,
    sort: str | Unset = UNSET,
    dir_: StackListDir | Unset = UNSET,
    recursive: bool | Unset = UNSET,
    ignore_duplicates: bool | Unset = UNSET,
    minimal: bool | Unset = UNSET,
    fields: str | Unset = UNSET,
) -> Response[StackListResponse200]:
    """List Documents in Stack

     Get a list of documents in a stack with pagination and filtering

    Args:
        stack_id (str | Unset): MongoDB ObjectId
        result_offset (float | Unset):  Default: 0.0.
        result_limit (float | Unset):  Default: 100.0.
        sort (str | Unset):
        dir_ (StackListDir | Unset):
        recursive (bool | Unset):
        ignore_duplicates (bool | Unset):
        minimal (bool | Unset):
        fields (str | Unset):

    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[StackListResponse200]
    """

    kwargs = _get_kwargs(
        stack_id=stack_id,
        result_offset=result_offset,
        result_limit=result_limit,
        sort=sort,
        dir_=dir_,
        recursive=recursive,
        ignore_duplicates=ignore_duplicates,
        minimal=minimal,
        fields=fields,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    *,
    client: AuthenticatedClient,
    stack_id: str | Unset = UNSET,
    result_offset: float | Unset = 0.0,
    result_limit: float | Unset = 100.0,
    sort: str | Unset = UNSET,
    dir_: StackListDir | Unset = UNSET,
    recursive: bool | Unset = UNSET,
    ignore_duplicates: bool | Unset = UNSET,
    minimal: bool | Unset = UNSET,
    fields: str | Unset = UNSET,
) -> StackListResponse200 | None:
    """List Documents in Stack

     Get a list of documents in a stack with pagination and filtering

    Args:
        stack_id (str | Unset): MongoDB ObjectId
        result_offset (float | Unset):  Default: 0.0.
        result_limit (float | Unset):  Default: 100.0.
        sort (str | Unset):
        dir_ (StackListDir | Unset):
        recursive (bool | Unset):
        ignore_duplicates (bool | Unset):
        minimal (bool | Unset):
        fields (str | Unset):

    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:
        StackListResponse200
    """

    return (
        await asyncio_detailed(
            client=client,
            stack_id=stack_id,
            result_offset=result_offset,
            result_limit=result_limit,
            sort=sort,
            dir_=dir_,
            recursive=recursive,
            ignore_duplicates=ignore_duplicates,
            minimal=minimal,
            fields=fields,
        )
    ).parsed
