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_compliance_scrape_response import PkgRoutesComplianceScrapeResponse
from ...models.post_compliance_client_plan_year_scrape_response_400 import PostComplianceClientPlanYearScrapeResponse400
from ...models.post_compliance_client_plan_year_scrape_response_403 import PostComplianceClientPlanYearScrapeResponse403
from ...models.post_compliance_client_plan_year_scrape_response_404 import PostComplianceClientPlanYearScrapeResponse404
from ...models.post_compliance_client_plan_year_scrape_response_500 import PostComplianceClientPlanYearScrapeResponse500
from ...types import Response


def _get_kwargs(
    client_path: str,
    plan: str,
    year: int,
) -> dict[str, Any]:

    _kwargs: dict[str, Any] = {
        "method": "post",
        "url": "/compliance/{client_path}/{plan}/{year}/_scrape".format(
            client_path=quote(str(client_path), safe=""),
            plan=quote(str(plan), safe=""),
            year=quote(str(year), safe=""),
        ),
    }

    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> (
    PkgRoutesComplianceScrapeResponse
    | PostComplianceClientPlanYearScrapeResponse400
    | PostComplianceClientPlanYearScrapeResponse403
    | PostComplianceClientPlanYearScrapeResponse404
    | PostComplianceClientPlanYearScrapeResponse500
    | None
):
    if response.status_code == 200:
        response_200 = PkgRoutesComplianceScrapeResponse.from_dict(response.json())

        return response_200

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

        return response_400

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

        return response_403

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

        return response_404

    if response.status_code == 500:
        response_500 = PostComplianceClientPlanYearScrapeResponse500.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[
    PkgRoutesComplianceScrapeResponse
    | PostComplianceClientPlanYearScrapeResponse400
    | PostComplianceClientPlanYearScrapeResponse403
    | PostComplianceClientPlanYearScrapeResponse404
    | PostComplianceClientPlanYearScrapeResponse500
]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    client_path: str,
    plan: str,
    year: int,
    *,
    client: AuthenticatedClient,
) -> Response[
    PkgRoutesComplianceScrapeResponse
    | PostComplianceClientPlanYearScrapeResponse400
    | PostComplianceClientPlanYearScrapeResponse403
    | PostComplianceClientPlanYearScrapeResponse404
    | PostComplianceClientPlanYearScrapeResponse500
]:
    """Scrape Form 5500 data

     Triggers scraping of Form 5500 data from EFAST for a specific client, plan, and year. Requires admin
    access. Sends a request to the EFAST extractor service with plan number, plan year, and EIN.

    Args:
        client_path (str):
        plan (str):
        year (int):

    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[PkgRoutesComplianceScrapeResponse | PostComplianceClientPlanYearScrapeResponse400 | PostComplianceClientPlanYearScrapeResponse403 | PostComplianceClientPlanYearScrapeResponse404 | PostComplianceClientPlanYearScrapeResponse500]
    """

    kwargs = _get_kwargs(
        client_path=client_path,
        plan=plan,
        year=year,
    )

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

    return _build_response(client=client, response=response)


def sync(
    client_path: str,
    plan: str,
    year: int,
    *,
    client: AuthenticatedClient,
) -> (
    PkgRoutesComplianceScrapeResponse
    | PostComplianceClientPlanYearScrapeResponse400
    | PostComplianceClientPlanYearScrapeResponse403
    | PostComplianceClientPlanYearScrapeResponse404
    | PostComplianceClientPlanYearScrapeResponse500
    | None
):
    """Scrape Form 5500 data

     Triggers scraping of Form 5500 data from EFAST for a specific client, plan, and year. Requires admin
    access. Sends a request to the EFAST extractor service with plan number, plan year, and EIN.

    Args:
        client_path (str):
        plan (str):
        year (int):

    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:
        PkgRoutesComplianceScrapeResponse | PostComplianceClientPlanYearScrapeResponse400 | PostComplianceClientPlanYearScrapeResponse403 | PostComplianceClientPlanYearScrapeResponse404 | PostComplianceClientPlanYearScrapeResponse500
    """

    return sync_detailed(
        client_path=client_path,
        plan=plan,
        year=year,
        client=client,
    ).parsed


async def asyncio_detailed(
    client_path: str,
    plan: str,
    year: int,
    *,
    client: AuthenticatedClient,
) -> Response[
    PkgRoutesComplianceScrapeResponse
    | PostComplianceClientPlanYearScrapeResponse400
    | PostComplianceClientPlanYearScrapeResponse403
    | PostComplianceClientPlanYearScrapeResponse404
    | PostComplianceClientPlanYearScrapeResponse500
]:
    """Scrape Form 5500 data

     Triggers scraping of Form 5500 data from EFAST for a specific client, plan, and year. Requires admin
    access. Sends a request to the EFAST extractor service with plan number, plan year, and EIN.

    Args:
        client_path (str):
        plan (str):
        year (int):

    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[PkgRoutesComplianceScrapeResponse | PostComplianceClientPlanYearScrapeResponse400 | PostComplianceClientPlanYearScrapeResponse403 | PostComplianceClientPlanYearScrapeResponse404 | PostComplianceClientPlanYearScrapeResponse500]
    """

    kwargs = _get_kwargs(
        client_path=client_path,
        plan=plan,
        year=year,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    client_path: str,
    plan: str,
    year: int,
    *,
    client: AuthenticatedClient,
) -> (
    PkgRoutesComplianceScrapeResponse
    | PostComplianceClientPlanYearScrapeResponse400
    | PostComplianceClientPlanYearScrapeResponse403
    | PostComplianceClientPlanYearScrapeResponse404
    | PostComplianceClientPlanYearScrapeResponse500
    | None
):
    """Scrape Form 5500 data

     Triggers scraping of Form 5500 data from EFAST for a specific client, plan, and year. Requires admin
    access. Sends a request to the EFAST extractor service with plan number, plan year, and EIN.

    Args:
        client_path (str):
        plan (str):
        year (int):

    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:
        PkgRoutesComplianceScrapeResponse | PostComplianceClientPlanYearScrapeResponse400 | PostComplianceClientPlanYearScrapeResponse403 | PostComplianceClientPlanYearScrapeResponse404 | PostComplianceClientPlanYearScrapeResponse500
    """

    return (
        await asyncio_detailed(
            client_path=client_path,
            plan=plan,
            year=year,
            client=client,
        )
    ).parsed
