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


class ProjectsClient(BaseClient):
    """Service client for Cloud Blueprint Projects 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 create_project(self, **kwargs):
        """Create project.

        All values come from the test layer via kwargs.
        """
        url = self.client.get_projects_path()
        post_body = json.dumps(kwargs)
        resp, body = self.post(url, post_body)
        return self._parse_response(
            resp, body, self.client.get_schema().create_project
        )

    def show_project(self):
        """Get project details using self.project_name."""
        url = self.client.get_project_path(self.project_name)
        resp, body = self.get(url)
        return self._parse_response(
            resp, body, self.client.get_schema().show_project
        )


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

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

    @abstractmethod
    def get_projects_path(self):
        """Return API endpoint path for creating projects."""
        pass

    @abstractmethod
    def get_project_path(self, project_name):
        """Return API endpoint path for a specific project."""
        pass
