from http import HTTPStatus
from typing import Any
from urllib.parse import quote

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.pkg_routes_cp_files_upload_complete_response import PkgRoutesCpFilesUploadCompleteResponse
from ...models.post_cp_upload_file_done_response_400 import PostCpUploadFileDoneResponse400
from ...models.post_cp_upload_file_done_response_404 import PostCpUploadFileDoneResponse404
from ...types import UNSET, Response, Unset


def _get_kwargs(
    file: str,
    *,
    error: str | Unset = UNSET,
) -> dict[str, Any]:

    params: dict[str, Any] = {}

    params["error"] = error

    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": "/cp/upload/{file}/done".format(
            file=quote(str(file), safe=""),
        ),
        "params": params,
    }

    return _kwargs


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

        return response_200

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

        return response_400

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

        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[
    PkgRoutesCpFilesUploadCompleteResponse | PostCpUploadFileDoneResponse400 | PostCpUploadFileDoneResponse404
]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    file: str,
    *,
    client: AuthenticatedClient | Client,
    error: str | Unset = UNSET,
) -> Response[
    PkgRoutesCpFilesUploadCompleteResponse | PostCpUploadFileDoneResponse400 | PostCpUploadFileDoneResponse404
]:
    """Mark upload as complete

     Mark a file upload as complete by updating modified_at timestamp. If error query parameter is
    provided, deletes the file and logs the error.

    Args:
        file (str):
        error (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[PkgRoutesCpFilesUploadCompleteResponse | PostCpUploadFileDoneResponse400 | PostCpUploadFileDoneResponse404]
    """

    kwargs = _get_kwargs(
        file=file,
        error=error,
    )

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

    return _build_response(client=client, response=response)


def sync(
    file: str,
    *,
    client: AuthenticatedClient | Client,
    error: str | Unset = UNSET,
) -> PkgRoutesCpFilesUploadCompleteResponse | PostCpUploadFileDoneResponse400 | PostCpUploadFileDoneResponse404 | None:
    """Mark upload as complete

     Mark a file upload as complete by updating modified_at timestamp. If error query parameter is
    provided, deletes the file and logs the error.

    Args:
        file (str):
        error (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:
        PkgRoutesCpFilesUploadCompleteResponse | PostCpUploadFileDoneResponse400 | PostCpUploadFileDoneResponse404
    """

    return sync_detailed(
        file=file,
        client=client,
        error=error,
    ).parsed


async def asyncio_detailed(
    file: str,
    *,
    client: AuthenticatedClient | Client,
    error: str | Unset = UNSET,
) -> Response[
    PkgRoutesCpFilesUploadCompleteResponse | PostCpUploadFileDoneResponse400 | PostCpUploadFileDoneResponse404
]:
    """Mark upload as complete

     Mark a file upload as complete by updating modified_at timestamp. If error query parameter is
    provided, deletes the file and logs the error.

    Args:
        file (str):
        error (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[PkgRoutesCpFilesUploadCompleteResponse | PostCpUploadFileDoneResponse400 | PostCpUploadFileDoneResponse404]
    """

    kwargs = _get_kwargs(
        file=file,
        error=error,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    file: str,
    *,
    client: AuthenticatedClient | Client,
    error: str | Unset = UNSET,
) -> PkgRoutesCpFilesUploadCompleteResponse | PostCpUploadFileDoneResponse400 | PostCpUploadFileDoneResponse404 | None:
    """Mark upload as complete

     Mark a file upload as complete by updating modified_at timestamp. If error query parameter is
    provided, deletes the file and logs the error.

    Args:
        file (str):
        error (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:
        PkgRoutesCpFilesUploadCompleteResponse | PostCpUploadFileDoneResponse400 | PostCpUploadFileDoneResponse404
    """

    return (
        await asyncio_detailed(
            file=file,
            client=client,
            error=error,
        )
    ).parsed
