# Copyright (c) 2025 Itential, Inc
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)


from typing import Sequence, Mapping, Any

from itential_mcp.platform.services import ServiceBase


class Service(ServiceBase):
    """
    Gateway Manager service for interacting with Itential Platform Gateway Manager.

    This service provides methods to discover and manage gateways and services
    within the Itential Platform Gateway Manager. Gateway Manager enables
    distributed service execution across multiple clusters and provides
    orchestration capabilities for external services.

    Attributes:
        name (str): The service name identifier "gateway_manager"
    """

    name: str = "gateway_manager"

    async def get_services(self) -> Sequence[Mapping[str, Any]]:
        """ """
        res = await self.client.get("/gateway_manager/v1/services")
        json_data = res.json()
        return json_data["result"]

    async def get_gateways(self) -> Sequence[Mapping[str, Any]]:
        """
        Get the list of all known gateways from Itential Platform Gateway Manager.

        Args:
            None

        Returns:
            Sequence[Mapping[str, Any]]: List of gateway objects with the following fields:
                - name: The gateway name
                - cluster: The cluster name
                - description: Short description of the gateway
                - status: Current status of the gateway connection
                - enabled: Whether or not the gateway is enabled and usable

        Raises:
            HTTPError: If the API request fails
        """
        res = await self.client.get("/gateway_manager/v1/gateways")
        return res.json()

    async def run_service(
        self, name: str, cluster: str, input_params: dict | None = None
    ) -> Mapping[str, Any]:
        """
        Run an existing service using the optional input parameters.

        Args:
            name (str): The name of the service to run
            cluster (str): The name of the cluster that owns the service
            input_params (dict | None): Optional input parameters to pass to the service

        Returns:
            Mapping[str, Any]: An object that represents output from the service
                with the following fields:
                    - stdout: The output sent to stdout
                    - stderr: The output sent to stderr
                    - return_code: The return code generated by the service
                    - start_time: The start time when the service was started
                    - end_time: The end time when the service run completed
                    - elapsed_time: The number of seconds the service ran for

        Raises:
            HTTPError: If the API request fails
            ValueError: If the service name or cluster is invalid
        """
        body = {
            "serviceName": name,
            "clusterId": cluster,
        }

        if input_params:
            body["params"] = input_params

        res = await self.client.post("/gateway_manager/v1/services/run", json=body)

        return res.json()
