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.post_dashboard_brief_brief_summary_task_response_400 import PostDashboardBriefBriefSummaryTaskResponse400
from ...models.post_dashboard_brief_brief_summary_task_response_404 import PostDashboardBriefBriefSummaryTaskResponse404
from ...models.post_dashboard_brief_brief_summary_task_response_500 import PostDashboardBriefBriefSummaryTaskResponse500
from ...models.types_task import TypesTask
from ...types import Response


def _get_kwargs(
    brief: str,
    summary: str,
    task: str,
) -> dict[str, Any]:

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

    return _kwargs


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

        return response_200

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

        return response_400

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

        return response_404

    if response.status_code == 500:
        response_500 = PostDashboardBriefBriefSummaryTaskResponse500.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[
    PostDashboardBriefBriefSummaryTaskResponse400
    | PostDashboardBriefBriefSummaryTaskResponse404
    | PostDashboardBriefBriefSummaryTaskResponse500
    | TypesTask
]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    brief: str,
    summary: str,
    task: str,
    *,
    client: AuthenticatedClient,
) -> Response[
    PostDashboardBriefBriefSummaryTaskResponse400
    | PostDashboardBriefBriefSummaryTaskResponse404
    | PostDashboardBriefBriefSummaryTaskResponse500
    | TypesTask
]:
    """Add suggested task from briefing

     Create a new task from a suggested task in a daily briefing. The task is created with the suggested
    description and associated with the client and message from the briefing.

    Args:
        brief (str):
        summary (str):
        task (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[PostDashboardBriefBriefSummaryTaskResponse400 | PostDashboardBriefBriefSummaryTaskResponse404 | PostDashboardBriefBriefSummaryTaskResponse500 | TypesTask]
    """

    kwargs = _get_kwargs(
        brief=brief,
        summary=summary,
        task=task,
    )

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

    return _build_response(client=client, response=response)


def sync(
    brief: str,
    summary: str,
    task: str,
    *,
    client: AuthenticatedClient,
) -> (
    PostDashboardBriefBriefSummaryTaskResponse400
    | PostDashboardBriefBriefSummaryTaskResponse404
    | PostDashboardBriefBriefSummaryTaskResponse500
    | TypesTask
    | None
):
    """Add suggested task from briefing

     Create a new task from a suggested task in a daily briefing. The task is created with the suggested
    description and associated with the client and message from the briefing.

    Args:
        brief (str):
        summary (str):
        task (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:
        PostDashboardBriefBriefSummaryTaskResponse400 | PostDashboardBriefBriefSummaryTaskResponse404 | PostDashboardBriefBriefSummaryTaskResponse500 | TypesTask
    """

    return sync_detailed(
        brief=brief,
        summary=summary,
        task=task,
        client=client,
    ).parsed


async def asyncio_detailed(
    brief: str,
    summary: str,
    task: str,
    *,
    client: AuthenticatedClient,
) -> Response[
    PostDashboardBriefBriefSummaryTaskResponse400
    | PostDashboardBriefBriefSummaryTaskResponse404
    | PostDashboardBriefBriefSummaryTaskResponse500
    | TypesTask
]:
    """Add suggested task from briefing

     Create a new task from a suggested task in a daily briefing. The task is created with the suggested
    description and associated with the client and message from the briefing.

    Args:
        brief (str):
        summary (str):
        task (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[PostDashboardBriefBriefSummaryTaskResponse400 | PostDashboardBriefBriefSummaryTaskResponse404 | PostDashboardBriefBriefSummaryTaskResponse500 | TypesTask]
    """

    kwargs = _get_kwargs(
        brief=brief,
        summary=summary,
        task=task,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    brief: str,
    summary: str,
    task: str,
    *,
    client: AuthenticatedClient,
) -> (
    PostDashboardBriefBriefSummaryTaskResponse400
    | PostDashboardBriefBriefSummaryTaskResponse404
    | PostDashboardBriefBriefSummaryTaskResponse500
    | TypesTask
    | None
):
    """Add suggested task from briefing

     Create a new task from a suggested task in a daily briefing. The task is created with the suggested
    description and associated with the client and message from the briefing.

    Args:
        brief (str):
        summary (str):
        task (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:
        PostDashboardBriefBriefSummaryTaskResponse400 | PostDashboardBriefBriefSummaryTaskResponse404 | PostDashboardBriefBriefSummaryTaskResponse500 | TypesTask
    """

    return (
        await asyncio_detailed(
            brief=brief,
            summary=summary,
            task=task,
            client=client,
        )
    ).parsed
