from http import HTTPStatus
from typing import Any, cast

import httpx

from peertube import errors
from peertube.client import AuthenticatedClient, Client
from peertube.models.request_two_factor_body import RequestTwoFactorBody
from peertube.models.request_two_factor_response import RequestTwoFactorResponse
from peertube.types import Response


def _get_kwargs(id: int, *, body: RequestTwoFactorBody) -> dict[str, Any]:
    headers: dict[str, Any] = {}

    _kwargs: dict[str, Any] = {
        "method": "post",
        "url": f"/api/v1/users/{id}/two-factor/request",
    }
    _kwargs["json"] = body.to_dict()

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

    _kwargs["headers"] = headers
    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> Any | list["RequestTwoFactorResponse"] | None:
    if response.status_code == 200:
        response_200 = []
        _response_200 = response.json()
        for response_200_item_data in _response_200:
            response_200_item = RequestTwoFactorResponse.from_dict(
                response_200_item_data
            )

            response_200.append(response_200_item)

        return response_200
    if response.status_code == 403:
        response_403 = cast("Any", None)
        return response_403
    if response.status_code == 404:
        response_404 = cast("Any", None)
        return response_404
    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[Any | list["RequestTwoFactorResponse"]]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    id: int, *, client: AuthenticatedClient, body: RequestTwoFactorBody
) -> Response[Any | list["RequestTwoFactorResponse"]]:
    """Request two factor auth

     Request two factor authentication for a user
    Args:
        id (int):  Example: 42.
        body (RequestTwoFactorBody): Request body data.

    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[Union[Any, list['RequestTwoFactorResponse']]]
    """

    kwargs = _get_kwargs(id=id, body=body)

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

    return _build_response(client=client, response=response)


def sync(
    id: int, *, client: AuthenticatedClient, body: RequestTwoFactorBody
) -> Any | list["RequestTwoFactorResponse"] | None:
    """Request two factor auth

     Request two factor authentication for a user
    Args:
        id (int):  Example: 42.
        body (RequestTwoFactorBody): Request body data.

    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:
        Union[Any, list['RequestTwoFactorResponse']]
    """

    return sync_detailed(id=id, client=client, body=body).parsed


async def asyncio_detailed(
    id: int, *, client: AuthenticatedClient, body: RequestTwoFactorBody
) -> Response[Any | list["RequestTwoFactorResponse"]]:
    """Request two factor auth

     Request two factor authentication for a user
    Args:
        id (int):  Example: 42.
        body (RequestTwoFactorBody): Request body data.

    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[Union[Any, list['RequestTwoFactorResponse']]]
    """

    kwargs = _get_kwargs(id=id, body=body)

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

    return _build_response(client=client, response=response)


async def asyncio(
    id: int, *, client: AuthenticatedClient, body: RequestTwoFactorBody
) -> Any | list["RequestTwoFactorResponse"] | None:
    """Request two factor auth

     Request two factor authentication for a user
    Args:
        id (int):  Example: 42.
        body (RequestTwoFactorBody): Request body data.

    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:
        Union[Any, list['RequestTwoFactorResponse']]
    """

    return (await asyncio_detailed(id=id, client=client, body=body)).parsed
