"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from .basesdk import BaseSDK
from gusto_app_integration import models, utils
from gusto_app_integration._hooks import HookContext
from gusto_app_integration.types import OptionalNullable, UNSET
from typing import Any, Mapping, Optional


class TimeOffPolicies(BaseSDK):
    def calculate_accruing_time_off_hours(
        self,
        *,
        payroll_id: str,
        employee_id: str,
        x_gusto_api_version: Optional[
            models.VersionHeader
        ] = models.VersionHeader.TWO_THOUSAND_AND_TWENTY_FOUR_MINUS_04_MINUS_01,
        regular_hours_worked: Optional[float] = None,
        overtime_hours_worked: Optional[float] = None,
        double_overtime_hours_worked: Optional[float] = None,
        pto_hours_used: Optional[float] = None,
        sick_hours_used: Optional[float] = None,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.AccruingTimeOffHourObject:
        r"""Calculate accruing time off hours

        Returns a list of accruing time off for each time off policy associated with the employee.

        Factors affecting the accrued hours:
        * the time off policy accrual method (whether they get pay per hour worked, per hour paid, with / without overtime, accumulate time off based on pay period / calendar year / anniversary)
        * how many hours of work during this pay period
        * how many hours of PTO / sick hours taken during this pay period (for per hour paid policies only)
        * company pay schedule frequency (for per pay period)

        If none of the parameters is passed in, the accrued time off hour will be 0.

        scope: `payrolls:read`

        :param payroll_id: The UUID of the payroll
        :param employee_id: The UUID of the employee
        :param x_gusto_api_version: Determines the date-based API version associated with your API call. If none is provided, your application's [minimum API version](https://docs.gusto.com/embedded-payroll/docs/api-versioning#minimum-api-version) is used.
        :param regular_hours_worked: regular hours worked in this pay period
        :param overtime_hours_worked: overtime hours worked in this pay period
        :param double_overtime_hours_worked: double overtime hours worked in this pay period
        :param pto_hours_used: paid time off hours used in this pay period
        :param sick_hours_used: sick hours used in this pay period
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.PostV1PayrollsPayrollIDCalculateAccruingTimeOffHoursRequest(
            payroll_id=payroll_id,
            employee_id=employee_id,
            x_gusto_api_version=x_gusto_api_version,
            request_body=models.PostV1PayrollsPayrollIDCalculateAccruingTimeOffHoursRequestBody(
                regular_hours_worked=regular_hours_worked,
                overtime_hours_worked=overtime_hours_worked,
                double_overtime_hours_worked=double_overtime_hours_worked,
                pto_hours_used=pto_hours_used,
                sick_hours_used=sick_hours_used,
            ),
        )

        req = self._build_request(
            method="POST",
            path="/v1/payrolls/{payroll_id}/employees/{employee_id}/calculate_accruing_time_off_hours",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=True,
            request_has_path_params=True,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(
                request.request_body,
                False,
                False,
                "json",
                models.PostV1PayrollsPayrollIDCalculateAccruingTimeOffHoursRequestBody,
            ),
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = self.do_request(
            hook_ctx=HookContext(
                base_url=base_url or "",
                operation_id="post-v1-payrolls-payroll_id-calculate_accruing_time_off_hours",
                oauth2_scopes=[],
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=["422", "4XX", "5XX"],
            retry_config=retry_config,
        )

        response_data: Any = None
        if utils.match_response(http_res, "200", "application/json"):
            return utils.unmarshal_json(http_res.text, models.AccruingTimeOffHourObject)
        if utils.match_response(http_res, "422", "application/json"):
            response_data = utils.unmarshal_json(
                http_res.text, models.UnprocessableEntityErrorObjectData
            )
            raise models.UnprocessableEntityErrorObject(data=response_data)
        if utils.match_response(http_res, "4XX", "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise models.APIError(
                "API error occurred", http_res.status_code, http_res_text, http_res
            )
        if utils.match_response(http_res, "5XX", "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise models.APIError(
                "API error occurred", http_res.status_code, http_res_text, http_res
            )

        content_type = http_res.headers.get("Content-Type")
        http_res_text = utils.stream_to_text(http_res)
        raise models.APIError(
            f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
            http_res.status_code,
            http_res_text,
            http_res,
        )

    async def calculate_accruing_time_off_hours_async(
        self,
        *,
        payroll_id: str,
        employee_id: str,
        x_gusto_api_version: Optional[
            models.VersionHeader
        ] = models.VersionHeader.TWO_THOUSAND_AND_TWENTY_FOUR_MINUS_04_MINUS_01,
        regular_hours_worked: Optional[float] = None,
        overtime_hours_worked: Optional[float] = None,
        double_overtime_hours_worked: Optional[float] = None,
        pto_hours_used: Optional[float] = None,
        sick_hours_used: Optional[float] = None,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.AccruingTimeOffHourObject:
        r"""Calculate accruing time off hours

        Returns a list of accruing time off for each time off policy associated with the employee.

        Factors affecting the accrued hours:
        * the time off policy accrual method (whether they get pay per hour worked, per hour paid, with / without overtime, accumulate time off based on pay period / calendar year / anniversary)
        * how many hours of work during this pay period
        * how many hours of PTO / sick hours taken during this pay period (for per hour paid policies only)
        * company pay schedule frequency (for per pay period)

        If none of the parameters is passed in, the accrued time off hour will be 0.

        scope: `payrolls:read`

        :param payroll_id: The UUID of the payroll
        :param employee_id: The UUID of the employee
        :param x_gusto_api_version: Determines the date-based API version associated with your API call. If none is provided, your application's [minimum API version](https://docs.gusto.com/embedded-payroll/docs/api-versioning#minimum-api-version) is used.
        :param regular_hours_worked: regular hours worked in this pay period
        :param overtime_hours_worked: overtime hours worked in this pay period
        :param double_overtime_hours_worked: double overtime hours worked in this pay period
        :param pto_hours_used: paid time off hours used in this pay period
        :param sick_hours_used: sick hours used in this pay period
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.PostV1PayrollsPayrollIDCalculateAccruingTimeOffHoursRequest(
            payroll_id=payroll_id,
            employee_id=employee_id,
            x_gusto_api_version=x_gusto_api_version,
            request_body=models.PostV1PayrollsPayrollIDCalculateAccruingTimeOffHoursRequestBody(
                regular_hours_worked=regular_hours_worked,
                overtime_hours_worked=overtime_hours_worked,
                double_overtime_hours_worked=double_overtime_hours_worked,
                pto_hours_used=pto_hours_used,
                sick_hours_used=sick_hours_used,
            ),
        )

        req = self._build_request_async(
            method="POST",
            path="/v1/payrolls/{payroll_id}/employees/{employee_id}/calculate_accruing_time_off_hours",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=True,
            request_has_path_params=True,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(
                request.request_body,
                False,
                False,
                "json",
                models.PostV1PayrollsPayrollIDCalculateAccruingTimeOffHoursRequestBody,
            ),
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = await self.do_request_async(
            hook_ctx=HookContext(
                base_url=base_url or "",
                operation_id="post-v1-payrolls-payroll_id-calculate_accruing_time_off_hours",
                oauth2_scopes=[],
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=["422", "4XX", "5XX"],
            retry_config=retry_config,
        )

        response_data: Any = None
        if utils.match_response(http_res, "200", "application/json"):
            return utils.unmarshal_json(http_res.text, models.AccruingTimeOffHourObject)
        if utils.match_response(http_res, "422", "application/json"):
            response_data = utils.unmarshal_json(
                http_res.text, models.UnprocessableEntityErrorObjectData
            )
            raise models.UnprocessableEntityErrorObject(data=response_data)
        if utils.match_response(http_res, "4XX", "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise models.APIError(
                "API error occurred", http_res.status_code, http_res_text, http_res
            )
        if utils.match_response(http_res, "5XX", "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise models.APIError(
                "API error occurred", http_res.status_code, http_res_text, http_res
            )

        content_type = http_res.headers.get("Content-Type")
        http_res_text = await utils.stream_to_text_async(http_res)
        raise models.APIError(
            f"Unexpected response received (code: {http_res.status_code}, type: {content_type})",
            http_res.status_code,
            http_res_text,
            http_res,
        )
