from gumband_python_sdk.service_name import ServiceName


class EndpointManager:
    """
    Manages API endpoints based on the provided environment.
    """

    service_ports = {
        ServiceName.AUTH: 3000,
        ServiceName.CONTENT: 3008,
        ServiceName.EXHIBIT: 3006,
        ServiceName.HARDWARE: 3012,
        ServiceName.LOG: 3007,
        ServiceName.NOTIFICATIONS: 3010,
        ServiceName.REPORTING: 3003,
        ServiceName.WS: 3002,
    }

    cloudEndpoints = {
        "local": "http://v2.localtest.me/api",
        "dev": "https://dev.gumband.com/api",
        "stag": "https://stag.gumband.com/api",
        "app": "https://app.gumband.com/api",
    }

    cloudWSEndpoints = {
        "local": "ws://v2.localtest.me/api",
        "dev": "wss://dev.gumband.com/api",
        "stag": "wss://stag.gumband.com/api",
        "app": "wss://app.gumband.com/api",
    }

    def __init__(self, environment: str, custom_server_ip: str = "0.0.0.0"):
        """
        Initializes the EndpointManager.

        :param environment: The current environment ('app', 'stag', 'dev', 'local', 'custom').
        """
        self.environment = environment.lower()
        self.custom_server_ip = custom_server_ip

    def get_endpoint(self, service_name, endpoint, endpoint_version="v1") -> str:
        if self.cloudEndpoints.get(self.environment):
            return f"{self.cloudEndpoints.get(self.environment)}/{endpoint_version}/{service_name.value}{endpoint}"

        if self.environment == "custom":
            return f"http://{self.custom_server_ip}/api/{endpoint_version}/{service_name.value}{endpoint}"

    def get_ws_endpoint(self, endpoint_version="v1") -> str:
        """
        Constructs the WebSocket endpoint URL.

        :return: Full WebSocket endpoint URL as a string.
        """
        if self.cloudWSEndpoints.get(self.environment):
            return f"{self.cloudWSEndpoints.get(self.environment)}/{endpoint_version}/ws"

        if self.environment == "custom":
            return f"ws://{self.custom_server_ip}/api/{endpoint_version}/ws"
