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_dashboard_brief_date_response_400 import GetDashboardBriefDateResponse400
from ...models.get_dashboard_brief_date_response_404 import GetDashboardBriefDateResponse404
from ...models.get_dashboard_brief_date_response_500 import GetDashboardBriefDateResponse500
from ...models.pkg_routes_dashboard_daily_briefing_response import PkgRoutesDashboardDailyBriefingResponse
from ...types import Response


def _get_kwargs(
    date: str,
) -> dict[str, Any]:

    _kwargs: dict[str, Any] = {
        "method": "get",
        "url": "/dashboard/brief/{date}".format(
            date=quote(str(date), safe=""),
        ),
    }

    return _kwargs


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

        return response_200

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

        return response_400

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

        return response_404

    if response.status_code == 500:
        response_500 = GetDashboardBriefDateResponse500.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[
    GetDashboardBriefDateResponse400
    | GetDashboardBriefDateResponse404
    | GetDashboardBriefDateResponse500
    | PkgRoutesDashboardDailyBriefingResponse
]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    date: str,
    *,
    client: AuthenticatedClient,
) -> Response[
    GetDashboardBriefDateResponse400
    | GetDashboardBriefDateResponse404
    | GetDashboardBriefDateResponse500
    | PkgRoutesDashboardDailyBriefingResponse
]:
    """Get daily briefing

     Get a daily briefing for a specific date (YYYY-MM-DD format) or 'latest' for the most recent
    briefing. Includes associated tasks and clients, plus previous and next briefing dates.

    Args:
        date (str):

    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[GetDashboardBriefDateResponse400 | GetDashboardBriefDateResponse404 | GetDashboardBriefDateResponse500 | PkgRoutesDashboardDailyBriefingResponse]
    """

    kwargs = _get_kwargs(
        date=date,
    )

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

    return _build_response(client=client, response=response)


def sync(
    date: str,
    *,
    client: AuthenticatedClient,
) -> (
    GetDashboardBriefDateResponse400
    | GetDashboardBriefDateResponse404
    | GetDashboardBriefDateResponse500
    | PkgRoutesDashboardDailyBriefingResponse
    | None
):
    """Get daily briefing

     Get a daily briefing for a specific date (YYYY-MM-DD format) or 'latest' for the most recent
    briefing. Includes associated tasks and clients, plus previous and next briefing dates.

    Args:
        date (str):

    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:
        GetDashboardBriefDateResponse400 | GetDashboardBriefDateResponse404 | GetDashboardBriefDateResponse500 | PkgRoutesDashboardDailyBriefingResponse
    """

    return sync_detailed(
        date=date,
        client=client,
    ).parsed


async def asyncio_detailed(
    date: str,
    *,
    client: AuthenticatedClient,
) -> Response[
    GetDashboardBriefDateResponse400
    | GetDashboardBriefDateResponse404
    | GetDashboardBriefDateResponse500
    | PkgRoutesDashboardDailyBriefingResponse
]:
    """Get daily briefing

     Get a daily briefing for a specific date (YYYY-MM-DD format) or 'latest' for the most recent
    briefing. Includes associated tasks and clients, plus previous and next briefing dates.

    Args:
        date (str):

    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[GetDashboardBriefDateResponse400 | GetDashboardBriefDateResponse404 | GetDashboardBriefDateResponse500 | PkgRoutesDashboardDailyBriefingResponse]
    """

    kwargs = _get_kwargs(
        date=date,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    date: str,
    *,
    client: AuthenticatedClient,
) -> (
    GetDashboardBriefDateResponse400
    | GetDashboardBriefDateResponse404
    | GetDashboardBriefDateResponse500
    | PkgRoutesDashboardDailyBriefingResponse
    | None
):
    """Get daily briefing

     Get a daily briefing for a specific date (YYYY-MM-DD format) or 'latest' for the most recent
    briefing. Includes associated tasks and clients, plus previous and next briefing dates.

    Args:
        date (str):

    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:
        GetDashboardBriefDateResponse400 | GetDashboardBriefDateResponse404 | GetDashboardBriefDateResponse500 | PkgRoutesDashboardDailyBriefingResponse
    """

    return (
        await asyncio_detailed(
            date=date,
            client=client,
        )
    ).parsed
