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


class MetadataClient(BaseClient):
    """Service client for Cloud Blueprint Metadata API"""

    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 list_tofu_versions(self):
        """Get OpenTofu version catalog metadata."""
        url = self.client.get_tofu_versions_path()
        resp, body = self.get(url)
        return self._parse_response(
            resp, body, self.client.get_schema().list_tofu_versions
        )


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

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

    @abstractmethod
    def get_tofu_versions_path(self):
        """Return API endpoint path for tofu versions metadata."""
        pass
