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.get_files_file_response_400 import GetFilesFileResponse400
from ...models.get_files_file_response_403 import GetFilesFileResponse403
from ...models.get_files_file_response_404 import GetFilesFileResponse404
from ...models.get_files_file_response_500 import GetFilesFileResponse500
from ...models.pkg_routes_files_get_file_response import PkgRoutesFilesGetFileResponse
from ...types import UNSET, Response, Unset


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

    params: dict[str, Any] = {}

    params["minimal"] = minimal

    params["meta"] = meta

    params["actual_size"] = actual_size

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

    return _kwargs


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

        return response_200

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

        return response_400

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

        return response_403

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

        return response_404

    if response.status_code == 500:
        response_500 = GetFilesFileResponse500.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[
    GetFilesFileResponse400
    | GetFilesFileResponse403
    | GetFilesFileResponse404
    | GetFilesFileResponse500
    | PkgRoutesFilesGetFileResponse
]:
    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,
    minimal: str | Unset = UNSET,
    meta: str | Unset = UNSET,
    actual_size: str | Unset = UNSET,
) -> Response[
    GetFilesFileResponse400
    | GetFilesFileResponse403
    | GetFilesFileResponse404
    | GetFilesFileResponse500
    | PkgRoutesFilesGetFileResponse
]:
    """Get file

     Get file details with presigned download URL, activity, comments, form data, and shared teams

    Args:
        file (str):
        minimal (str | Unset):
        meta (str | Unset):
        actual_size (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[GetFilesFileResponse400 | GetFilesFileResponse403 | GetFilesFileResponse404 | GetFilesFileResponse500 | PkgRoutesFilesGetFileResponse]
    """

    kwargs = _get_kwargs(
        file=file,
        minimal=minimal,
        meta=meta,
        actual_size=actual_size,
    )

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

    return _build_response(client=client, response=response)


def sync(
    file: str,
    *,
    client: AuthenticatedClient,
    minimal: str | Unset = UNSET,
    meta: str | Unset = UNSET,
    actual_size: str | Unset = UNSET,
) -> (
    GetFilesFileResponse400
    | GetFilesFileResponse403
    | GetFilesFileResponse404
    | GetFilesFileResponse500
    | PkgRoutesFilesGetFileResponse
    | None
):
    """Get file

     Get file details with presigned download URL, activity, comments, form data, and shared teams

    Args:
        file (str):
        minimal (str | Unset):
        meta (str | Unset):
        actual_size (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:
        GetFilesFileResponse400 | GetFilesFileResponse403 | GetFilesFileResponse404 | GetFilesFileResponse500 | PkgRoutesFilesGetFileResponse
    """

    return sync_detailed(
        file=file,
        client=client,
        minimal=minimal,
        meta=meta,
        actual_size=actual_size,
    ).parsed


async def asyncio_detailed(
    file: str,
    *,
    client: AuthenticatedClient,
    minimal: str | Unset = UNSET,
    meta: str | Unset = UNSET,
    actual_size: str | Unset = UNSET,
) -> Response[
    GetFilesFileResponse400
    | GetFilesFileResponse403
    | GetFilesFileResponse404
    | GetFilesFileResponse500
    | PkgRoutesFilesGetFileResponse
]:
    """Get file

     Get file details with presigned download URL, activity, comments, form data, and shared teams

    Args:
        file (str):
        minimal (str | Unset):
        meta (str | Unset):
        actual_size (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[GetFilesFileResponse400 | GetFilesFileResponse403 | GetFilesFileResponse404 | GetFilesFileResponse500 | PkgRoutesFilesGetFileResponse]
    """

    kwargs = _get_kwargs(
        file=file,
        minimal=minimal,
        meta=meta,
        actual_size=actual_size,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    file: str,
    *,
    client: AuthenticatedClient,
    minimal: str | Unset = UNSET,
    meta: str | Unset = UNSET,
    actual_size: str | Unset = UNSET,
) -> (
    GetFilesFileResponse400
    | GetFilesFileResponse403
    | GetFilesFileResponse404
    | GetFilesFileResponse500
    | PkgRoutesFilesGetFileResponse
    | None
):
    """Get file

     Get file details with presigned download URL, activity, comments, form data, and shared teams

    Args:
        file (str):
        minimal (str | Unset):
        meta (str | Unset):
        actual_size (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:
        GetFilesFileResponse400 | GetFilesFileResponse403 | GetFilesFileResponse404 | GetFilesFileResponse500 | PkgRoutesFilesGetFileResponse
    """

    return (
        await asyncio_detailed(
            file=file,
            client=client,
            minimal=minimal,
            meta=meta,
            actual_size=actual_size,
        )
    ).parsed
