from __future__ import annotations

from http import HTTPStatus
from typing import Any

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.get_reservations_response import GetReservationsResponse
from ...models.get_reservations_v2_reservation_get_sort_by import (
    GetReservationsV2ReservationGetSortBy,
)
from ...models.get_reservations_v2_reservation_get_status import (
    GetReservationsV2ReservationGetStatus,
)
from ...models.http_validation_error import HTTPValidationError
from ...models.sort_direction import SortDirection
from ...types import UNSET, Response, Unset


def _get_kwargs(
    *,
    next_cursor: Any | Unset = UNSET,
    sort_by: GetReservationsV2ReservationGetSortBy | Unset = UNSET,
    sort_dir: SortDirection | Unset = UNSET,
    project: str,
    instance_type: None | str | Unset = UNSET,
    region: None | str | Unset = UNSET,
    status: GetReservationsV2ReservationGetStatus | Unset = UNSET,
    limit: int | None | Unset = UNSET,
) -> dict[str, Any]:
    params: dict[str, Any] = {}

    params["next_cursor"] = next_cursor

    json_sort_by: str | Unset = UNSET
    if not isinstance(sort_by, Unset):
        json_sort_by = sort_by.value

    params["sort_by"] = json_sort_by

    json_sort_dir: str | Unset = UNSET
    if not isinstance(sort_dir, Unset):
        json_sort_dir = sort_dir.value

    params["sort_dir"] = json_sort_dir

    params["project"] = project

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

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

    json_status: str | Unset = UNSET
    if not isinstance(status, Unset):
        json_status = status.value

    params["status"] = json_status

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

    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": "/v2/reservation",
        "params": params,
    }

    return _kwargs


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

        return response_200

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

        return response_422

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


def _build_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> Response[GetReservationsResponse | HTTPValidationError]:
    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,
    next_cursor: Any | Unset = UNSET,
    sort_by: GetReservationsV2ReservationGetSortBy | Unset = UNSET,
    sort_dir: SortDirection | Unset = UNSET,
    project: str,
    instance_type: None | str | Unset = UNSET,
    region: None | str | Unset = UNSET,
    status: GetReservationsV2ReservationGetStatus | Unset = UNSET,
    limit: int | None | Unset = UNSET,
) -> Response[GetReservationsResponse | HTTPValidationError]:
    """Get Reservations

     Get all reservations for a project

    Args:
        next_cursor (Any | Unset):
        sort_by (GetReservationsV2ReservationGetSortBy | Unset):
        sort_dir (SortDirection | Unset):
        project (str):
        instance_type (None | str | Unset):
        region (None | str | Unset):
        status (GetReservationsV2ReservationGetStatus | Unset):
        limit (int | None | 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[GetReservationsResponse | HTTPValidationError]
    """
    kwargs = _get_kwargs(
        next_cursor=next_cursor,
        sort_by=sort_by,
        sort_dir=sort_dir,
        project=project,
        instance_type=instance_type,
        region=region,
        status=status,
        limit=limit,
    )

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

    return _build_response(client=client, response=response)


def sync(
    *,
    client: AuthenticatedClient,
    next_cursor: Any | Unset = UNSET,
    sort_by: GetReservationsV2ReservationGetSortBy | Unset = UNSET,
    sort_dir: SortDirection | Unset = UNSET,
    project: str,
    instance_type: None | str | Unset = UNSET,
    region: None | str | Unset = UNSET,
    status: GetReservationsV2ReservationGetStatus | Unset = UNSET,
    limit: int | None | Unset = UNSET,
) -> GetReservationsResponse | HTTPValidationError | None:
    """Get Reservations

     Get all reservations for a project

    Args:
        next_cursor (Any | Unset):
        sort_by (GetReservationsV2ReservationGetSortBy | Unset):
        sort_dir (SortDirection | Unset):
        project (str):
        instance_type (None | str | Unset):
        region (None | str | Unset):
        status (GetReservationsV2ReservationGetStatus | Unset):
        limit (int | None | 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:
        GetReservationsResponse | HTTPValidationError
    """
    return sync_detailed(
        client=client,
        next_cursor=next_cursor,
        sort_by=sort_by,
        sort_dir=sort_dir,
        project=project,
        instance_type=instance_type,
        region=region,
        status=status,
        limit=limit,
    ).parsed


async def asyncio_detailed(
    *,
    client: AuthenticatedClient,
    next_cursor: Any | Unset = UNSET,
    sort_by: GetReservationsV2ReservationGetSortBy | Unset = UNSET,
    sort_dir: SortDirection | Unset = UNSET,
    project: str,
    instance_type: None | str | Unset = UNSET,
    region: None | str | Unset = UNSET,
    status: GetReservationsV2ReservationGetStatus | Unset = UNSET,
    limit: int | None | Unset = UNSET,
) -> Response[GetReservationsResponse | HTTPValidationError]:
    """Get Reservations

     Get all reservations for a project

    Args:
        next_cursor (Any | Unset):
        sort_by (GetReservationsV2ReservationGetSortBy | Unset):
        sort_dir (SortDirection | Unset):
        project (str):
        instance_type (None | str | Unset):
        region (None | str | Unset):
        status (GetReservationsV2ReservationGetStatus | Unset):
        limit (int | None | 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[GetReservationsResponse | HTTPValidationError]
    """
    kwargs = _get_kwargs(
        next_cursor=next_cursor,
        sort_by=sort_by,
        sort_dir=sort_dir,
        project=project,
        instance_type=instance_type,
        region=region,
        status=status,
        limit=limit,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    *,
    client: AuthenticatedClient,
    next_cursor: Any | Unset = UNSET,
    sort_by: GetReservationsV2ReservationGetSortBy | Unset = UNSET,
    sort_dir: SortDirection | Unset = UNSET,
    project: str,
    instance_type: None | str | Unset = UNSET,
    region: None | str | Unset = UNSET,
    status: GetReservationsV2ReservationGetStatus | Unset = UNSET,
    limit: int | None | Unset = UNSET,
) -> GetReservationsResponse | HTTPValidationError | None:
    """Get Reservations

     Get all reservations for a project

    Args:
        next_cursor (Any | Unset):
        sort_by (GetReservationsV2ReservationGetSortBy | Unset):
        sort_dir (SortDirection | Unset):
        project (str):
        instance_type (None | str | Unset):
        region (None | str | Unset):
        status (GetReservationsV2ReservationGetStatus | Unset):
        limit (int | None | 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:
        GetReservationsResponse | HTTPValidationError
    """
    return (
        await asyncio_detailed(
            client=client,
            next_cursor=next_cursor,
            sort_by=sort_by,
            sort_dir=sort_dir,
            project=project,
            instance_type=instance_type,
            region=region,
            status=status,
            limit=limit,
        )
    ).parsed
