# This file was generated by liblab | https://liblab.com/

from .utils.validator import Validator
from .utils.base_service import BaseService
from ..net.transport.serializer import Serializer
from ..models.utils.cast_models import cast_models
from ..models.custom_fields_list_container import CustomFieldsListContainer
from ..models.custom_field_values_container import CustomFieldValuesContainer
from ..models.custom_field_to_update_container import CustomFieldToUpdateContainer
from ..models.custom_field_item_container import CustomFieldItemContainer


class CustomFieldsService(BaseService):

    @cast_models
    def get_custom_fields_from_people(self) -> CustomFieldsListContainer:
        """Fetch all custom fields associated with People records, offering additional personalized information.

        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        :return: Successful response.
        :rtype: CustomFieldsListContainer
        """

        serialized_request = (
            Serializer(
                f"{self.base_url}/people/custom_fields", self.get_default_headers()
            )
            .serialize()
            .set_method("GET")
        )

        response = self.send_request(serialized_request)

        return CustomFieldsListContainer._unmap(response)

    @cast_models
    def get_custom_field_from_people_by_id(
        self, field_id: str
    ) -> CustomFieldItemContainer:
        """Access details of a specific custom field by using the custom field ID to enrich individual personnel records.

        :param field_id: Custom field id.
        :type field_id: str
        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        :return: Successful response.
        :rtype: CustomFieldItemContainer
        """

        Validator(str).validate(field_id)

        serialized_request = (
            Serializer(
                f"{self.base_url}/people/custom_fields/{{field_id}}",
                self.get_default_headers(),
            )
            .add_path("field_id", field_id)
            .serialize()
            .set_method("GET")
        )

        response = self.send_request(serialized_request)

        return CustomFieldItemContainer._unmap(response)

    @cast_models
    def get_custom_field_values_from_worker(
        self, worker_id: str
    ) -> CustomFieldValuesContainer:
        """Obtain all custom field values associated with a specific worker, facilitating detailed data retrieval for administrative or operational purposes.

        :param worker_id: Worker id.
        :type worker_id: str
        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        :return: Successful response.
        :rtype: CustomFieldValuesContainer
        """

        Validator(str).validate(worker_id)

        serialized_request = (
            Serializer(
                f"{self.base_url}/people/{{worker_id}}/custom_fields",
                self.get_default_headers(),
            )
            .add_path("worker_id", worker_id)
            .serialize()
            .set_method("GET")
        )

        response = self.send_request(serialized_request)

        return CustomFieldValuesContainer._unmap(response)

    @cast_models
    def upsert_custom_field_value_from_worker(
        self, request_body: CustomFieldToUpdateContainer, worker_id: str
    ):
        """Add or update custom field values for a specific worker, ensuring their profile is up-to-date with the latest information.

        :param request_body: The request body.
        :type request_body: CustomFieldToUpdateContainer
        :param worker_id: Worker id.
        :type worker_id: str
        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        """

        Validator(CustomFieldToUpdateContainer).validate(request_body)
        Validator(str).validate(worker_id)

        serialized_request = (
            Serializer(
                f"{self.base_url}/people/{{worker_id}}/custom_fields",
                self.get_default_headers(),
            )
            .add_path("worker_id", worker_id)
            .serialize()
            .set_method("PUT")
            .set_body(request_body)
        )

        response = self.send_request(serialized_request)

        return response

    @cast_models
    def delete_custom_field_value_from_worker(self, worker_id: str, field_id: str):
        """Delete a specific custom field value from a worker's profile, removing outdated or unnecessary information.

        :param worker_id: Worker id.
        :type worker_id: str
        :param field_id: Custom field id.
        :type field_id: str
        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        """

        Validator(str).validate(worker_id)
        Validator(str).validate(field_id)

        serialized_request = (
            Serializer(
                f"{self.base_url}/people/{{worker_id}}/custom_fields/{{field_id}}",
                self.get_default_headers(),
            )
            .add_path("worker_id", worker_id)
            .add_path("field_id", field_id)
            .serialize()
            .set_method("DELETE")
        )

        response = self.send_request(serialized_request)

        return response

    @cast_models
    def get_custom_fields_from_contracts(self) -> CustomFieldsListContainer:
        """Fetch all custom fields associated with contracts, providing additional data necessary for contract management.

        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        :return: Successful response.
        :rtype: CustomFieldsListContainer
        """

        serialized_request = (
            Serializer(
                f"{self.base_url}/contracts/custom_fields", self.get_default_headers()
            )
            .serialize()
            .set_method("GET")
        )

        response = self.send_request(serialized_request)

        return CustomFieldsListContainer._unmap(response)

    @cast_models
    def get_custom_field_from_contracts_by_id(
        self, field_id: str
    ) -> CustomFieldItemContainer:
        """Access details of a specific custom field associated with a contract to manage contract-specific attributes effectively.

        :param field_id: Custom field id.
        :type field_id: str
        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        :return: Successful response.
        :rtype: CustomFieldItemContainer
        """

        Validator(str).validate(field_id)

        serialized_request = (
            Serializer(
                f"{self.base_url}/contracts/custom_fields/{{field_id}}",
                self.get_default_headers(),
            )
            .add_path("field_id", field_id)
            .serialize()
            .set_method("GET")
        )

        response = self.send_request(serialized_request)

        return CustomFieldItemContainer._unmap(response)

    @cast_models
    def get_custom_field_values_from_contract(
        self, contract_id: str
    ) -> CustomFieldValuesContainer:
        """Obtain all custom field values for a specific contract, enhancing contract management with detailed, custom-configured data.

        :param contract_id: Contract id.
        :type contract_id: str
        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        :return: Successful response.
        :rtype: CustomFieldValuesContainer
        """

        Validator(str).validate(contract_id)

        serialized_request = (
            Serializer(
                f"{self.base_url}/contracts/{{contract_id}}/custom_fields",
                self.get_default_headers(),
            )
            .add_path("contract_id", contract_id)
            .serialize()
            .set_method("GET")
        )

        response = self.send_request(serialized_request)

        return CustomFieldValuesContainer._unmap(response)

    @cast_models
    def upsert_custom_field_value_from_contracts(
        self, request_body: CustomFieldToUpdateContainer, contract_id: str
    ):
        """Add or update custom field values for a specific contract, ensuring all contract details are current and relevant.

        :param request_body: The request body.
        :type request_body: CustomFieldToUpdateContainer
        :param contract_id: Contract id.
        :type contract_id: str
        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        """

        Validator(CustomFieldToUpdateContainer).validate(request_body)
        Validator(str).validate(contract_id)

        serialized_request = (
            Serializer(
                f"{self.base_url}/contracts/{{contract_id}}/custom_fields",
                self.get_default_headers(),
            )
            .add_path("contract_id", contract_id)
            .serialize()
            .set_method("PUT")
            .set_body(request_body)
        )

        response = self.send_request(serialized_request)

        return response

    @cast_models
    def delete_contract_custom_field(self, contract_id: str, field_id: str):
        """Delete a specific custom field’s value from a contract, streamlining contract data management by removing redundant or outdated information.

        :param contract_id: Contract id.
        :type contract_id: str
        :param field_id: Custom field id.
        :type field_id: str
        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        """

        Validator(str).validate(contract_id)
        Validator(str).validate(field_id)

        serialized_request = (
            Serializer(
                f"{self.base_url}/contracts/{{contract_id}}/custom_fields/{{field_id}}",
                self.get_default_headers(),
            )
            .add_path("contract_id", contract_id)
            .add_path("field_id", field_id)
            .serialize()
            .set_method("DELETE")
        )

        response = self.send_request(serialized_request)

        return response
