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.patch_packages_retirement_plans_plan_body import PatchPackagesRetirementPlansPlanBody
from ...models.patch_packages_retirement_plans_plan_response_400 import PatchPackagesRetirementPlansPlanResponse400
from ...models.patch_packages_retirement_plans_plan_response_403 import PatchPackagesRetirementPlansPlanResponse403
from ...models.patch_packages_retirement_plans_plan_response_404 import PatchPackagesRetirementPlansPlanResponse404
from ...models.patch_packages_retirement_plans_plan_response_500 import PatchPackagesRetirementPlansPlanResponse500
from ...models.pkg_routes_packages_retirement_plans_update_plan_response import (
    PkgRoutesPackagesRetirementPlansUpdatePlanResponse,
)
from ...types import Response


def _get_kwargs(
    plan: str,
    *,
    body: PatchPackagesRetirementPlansPlanBody,
) -> dict[str, Any]:
    headers: dict[str, Any] = {}

    _kwargs: dict[str, Any] = {
        "method": "patch",
        "url": "/packages/retirement/plans/{plan}".format(
            plan=quote(str(plan), safe=""),
        ),
    }

    _kwargs["json"] = body.to_dict()

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

    _kwargs["headers"] = headers
    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> (
    PatchPackagesRetirementPlansPlanResponse400
    | PatchPackagesRetirementPlansPlanResponse403
    | PatchPackagesRetirementPlansPlanResponse404
    | PatchPackagesRetirementPlansPlanResponse500
    | PkgRoutesPackagesRetirementPlansUpdatePlanResponse
    | None
):
    if response.status_code == 200:
        response_200 = PkgRoutesPackagesRetirementPlansUpdatePlanResponse.from_dict(response.json())

        return response_200

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

        return response_400

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

        return response_403

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

        return response_404

    if response.status_code == 500:
        response_500 = PatchPackagesRetirementPlansPlanResponse500.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[
    PatchPackagesRetirementPlansPlanResponse400
    | PatchPackagesRetirementPlansPlanResponse403
    | PatchPackagesRetirementPlansPlanResponse404
    | PatchPackagesRetirementPlansPlanResponse500
    | PkgRoutesPackagesRetirementPlansUpdatePlanResponse
]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    plan: str,
    *,
    client: AuthenticatedClient,
    body: PatchPackagesRetirementPlansPlanBody,
) -> Response[
    PatchPackagesRetirementPlansPlanResponse400
    | PatchPackagesRetirementPlansPlanResponse403
    | PatchPackagesRetirementPlansPlanResponse404
    | PatchPackagesRetirementPlansPlanResponse500
    | PkgRoutesPackagesRetirementPlansUpdatePlanResponse
]:
    """Update retirement plan

     Update retirement plan properties including name, status, notes, code, type, plan year end, tax year
    end, carrier, client, assignees, tags, and fields. Updates joinder plans if status changes. Sends
    notifications for mentions in notes.

    Args:
        plan (str):
        body (PatchPackagesRetirementPlansPlanBody):

    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[PatchPackagesRetirementPlansPlanResponse400 | PatchPackagesRetirementPlansPlanResponse403 | PatchPackagesRetirementPlansPlanResponse404 | PatchPackagesRetirementPlansPlanResponse500 | PkgRoutesPackagesRetirementPlansUpdatePlanResponse]
    """

    kwargs = _get_kwargs(
        plan=plan,
        body=body,
    )

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

    return _build_response(client=client, response=response)


def sync(
    plan: str,
    *,
    client: AuthenticatedClient,
    body: PatchPackagesRetirementPlansPlanBody,
) -> (
    PatchPackagesRetirementPlansPlanResponse400
    | PatchPackagesRetirementPlansPlanResponse403
    | PatchPackagesRetirementPlansPlanResponse404
    | PatchPackagesRetirementPlansPlanResponse500
    | PkgRoutesPackagesRetirementPlansUpdatePlanResponse
    | None
):
    """Update retirement plan

     Update retirement plan properties including name, status, notes, code, type, plan year end, tax year
    end, carrier, client, assignees, tags, and fields. Updates joinder plans if status changes. Sends
    notifications for mentions in notes.

    Args:
        plan (str):
        body (PatchPackagesRetirementPlansPlanBody):

    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:
        PatchPackagesRetirementPlansPlanResponse400 | PatchPackagesRetirementPlansPlanResponse403 | PatchPackagesRetirementPlansPlanResponse404 | PatchPackagesRetirementPlansPlanResponse500 | PkgRoutesPackagesRetirementPlansUpdatePlanResponse
    """

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


async def asyncio_detailed(
    plan: str,
    *,
    client: AuthenticatedClient,
    body: PatchPackagesRetirementPlansPlanBody,
) -> Response[
    PatchPackagesRetirementPlansPlanResponse400
    | PatchPackagesRetirementPlansPlanResponse403
    | PatchPackagesRetirementPlansPlanResponse404
    | PatchPackagesRetirementPlansPlanResponse500
    | PkgRoutesPackagesRetirementPlansUpdatePlanResponse
]:
    """Update retirement plan

     Update retirement plan properties including name, status, notes, code, type, plan year end, tax year
    end, carrier, client, assignees, tags, and fields. Updates joinder plans if status changes. Sends
    notifications for mentions in notes.

    Args:
        plan (str):
        body (PatchPackagesRetirementPlansPlanBody):

    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[PatchPackagesRetirementPlansPlanResponse400 | PatchPackagesRetirementPlansPlanResponse403 | PatchPackagesRetirementPlansPlanResponse404 | PatchPackagesRetirementPlansPlanResponse500 | PkgRoutesPackagesRetirementPlansUpdatePlanResponse]
    """

    kwargs = _get_kwargs(
        plan=plan,
        body=body,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    plan: str,
    *,
    client: AuthenticatedClient,
    body: PatchPackagesRetirementPlansPlanBody,
) -> (
    PatchPackagesRetirementPlansPlanResponse400
    | PatchPackagesRetirementPlansPlanResponse403
    | PatchPackagesRetirementPlansPlanResponse404
    | PatchPackagesRetirementPlansPlanResponse500
    | PkgRoutesPackagesRetirementPlansUpdatePlanResponse
    | None
):
    """Update retirement plan

     Update retirement plan properties including name, status, notes, code, type, plan year end, tax year
    end, carrier, client, assignees, tags, and fields. Updates joinder plans if status changes. Sends
    notifications for mentions in notes.

    Args:
        plan (str):
        body (PatchPackagesRetirementPlansPlanBody):

    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:
        PatchPackagesRetirementPlansPlanResponse400 | PatchPackagesRetirementPlansPlanResponse403 | PatchPackagesRetirementPlansPlanResponse404 | PatchPackagesRetirementPlansPlanResponse500 | PkgRoutesPackagesRetirementPlansUpdatePlanResponse
    """

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