import json
from abc import ABC, abstractmethod
from lib.common import rest_client
from lib.services.rollouts.base_client import BaseClient


class StatusClient(BaseClient):
    """Service client for Rollouts Status API"""

    api_version = "v1"

    def __init__(self, auth_provider, client, **kwargs):
        super().__init__(auth_provider=auth_provider, **kwargs)
        self.client = client

    def _parse_response(self, resp, body, schema):
        """Parse and validate response."""
        if not body or body.strip() == '':
            body_data = {}
        else:
            body_data = json.loads(body)
        self.validate_response(schema, resp, body_data)
        return rest_client.ResponseBody(resp, body_data)

    def show_status(self, name, product):
        """Retrieve the current status of a specific rollout configuration."""
        url = self.client.get_status_path(self.project_name, product, name)
        resp, body = self.get(url)
        return self._parse_response(
            resp, body, self.client.get_schema().show_status
        )


class StatusClientInterface(ABC):
    """Interface for cloud-specific implementations."""

    @abstractmethod
    def get_schema(self):
        """Return response schemas module."""
        pass

    @abstractmethod
    def get_status_path(self, project, product, name):
        """Return API endpoint path for rollout status."""
        pass
