from gumband_python_sdk.logging_handler import get_local_logger
from gumband_python_sdk.network_handler import NetworkHandler
from gumband_python_sdk.service_name import ServiceName

SERVICE = ServiceName.REPORTING

local_logger = get_local_logger()


class UserEventService:
    """
    UserEventService
    =================
    A service class for managing user interaction events for an exhibit component.
    This class is intended as a private class. Please use the Gumband service instead.
    """

    def __init__(
        self,
        exhibit_id: int,
        network_handler: NetworkHandler,
    ):
        """
        Initializes the UserEventService.

        Args:
            exhibit_id (int): The ID of the exhibit.
            network_handler (NetworkHandler): An instance of the network handler for API communication.
        """
        self.exhibit_id = exhibit_id
        self.network_handler = network_handler

    async def set_metrics_configuration(self, interaction_timeout_ms: int = 60000):
        """
        Configures interaction timeout (in milliseconds) for the exhibit component.

        Args:
            interaction_timeout_ms (int, optional): The interaction timeout in milliseconds.
                Defaults to 60000 (1 minute).

        Raises:
            Exception: If the configuration update fails.
        """
        try:
            endpoint = "/exhibit/broadcastEventConfigs"
            data = {
                "exhibitId": self.exhibit_id,
                "manifest": {"metrics": [{"interactionTimeoutMs": interaction_timeout_ms}]},
            }
            await self.network_handler.post(service=SERVICE, endpoint=endpoint, data=data)
            local_logger.info("Metrics configuration updated successfully.")
        except Exception:
            raise

    async def create_event(self, event_name: str, event_data: dict):
        """
        Sends a custom user event to the Gumband API.

        Args:
            event_name (str): The name of the event.
            event_data (dict): A dictionary containing event-specific data.

        Returns:
            dict: The server's response to the event.

        Raises:
            Exception: If the event creation fails.
        """
        try:
            endpoint = "/exhibit/metric"
            data = {"exhibitId": self.exhibit_id, "name": event_name, "data": event_data}
            return await self.network_handler.post(service=SERVICE, endpoint=endpoint, data=data)
        except Exception:
            raise

    async def end_interaction(self, event_data: dict):
        """
        Sends an "EndInteraction" event to the Gumband API, indicating the end of a user interaction.

        Args:
            event_data (dict): A dictionary containing event-specific data.

        Returns:
            dict: The server's response to the event.

        Raises:
            Exception: If the event submission fails.
        """
        try:
            endpoint = "/exhibit/metric"
            data = {"exhibitId": self.exhibit_id, "name": "EndInteraction", "data": event_data}
            return await self.network_handler.post(service=SERVICE, endpoint=endpoint, data=data)
        except Exception:
            raise
