import json
from urllib import parse as urllib
from abc import ABC, abstractmethod
from lib.common import rest_client
from lib.services.api_gateway.base_client import BaseClient


class KeysClient(BaseClient):
    """Service client for API Gateway Keys resource."""

    def __init__(self, auth_provider, service, region, client=None, **kwargs):
        super(KeysClient, self).__init__(auth_provider, service, region, **kwargs)
        self.client = client

    def _build_url(self, key_id=None):
        """Build URL for key operations."""
        url = self.client.get_keys_path(self.project_name)
        if key_id:
            url = f"{url}/{key_id}"
        return url

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

    def list_keys(self, **params):
        """List API keys."""
        url = self._build_url()
        if params:
            url += f"?{urllib.urlencode(params)}"
        resp, body = self.get(url)
        return self._parse_response(
            resp, body, self.client.get_schema().list_keys
        )

    def create_key(self, **kwargs):
        """Create an API key."""
        url = self._build_url()
        post_body = json.dumps(kwargs)
        resp, body = self.post(url, post_body)
        return self._parse_response(
            resp, body, self.client.get_schema().create_key
        )

    def show_key(self, key_id):
        """Show API key details."""
        url = self._build_url(key_id)
        resp, body = self.get(url)
        return self._parse_response(
            resp, body, self.client.get_schema().show_key
        )

    def update_key(self, key_id, if_match=None, **kwargs):
        """Update an API key."""
        url = self._build_url(key_id)
        headers = {"If-Match": if_match} if if_match else None
        patch_body = json.dumps(kwargs)
        resp, body = self.patch(url, patch_body, headers=headers, extra_headers=True)
        return self._parse_response(
            resp, body, self.client.get_schema().update_key
        )

    def delete_key(self, key_id, if_match=None):
        """Delete an API key."""
        url = self._build_url(key_id)
        headers = {"If-Match": if_match} if if_match else None
        resp, body = self.delete(url, headers=headers, extra_headers=True)
        return self._parse_response(
            resp, body, self.client.get_schema().delete_key
        )

    def list_key_keysets(self, key_id, **params):
        """List keysets containing a key."""
        url = self.client.get_key_keysets_path(self.project_name, key_id)
        if params:
            url += f"?{urllib.urlencode(params)}"
        resp, body = self.get(url)
        return self._parse_response(
            resp, body, self.client.get_schema().list_key_keysets
        )

    def update_key_keysets(self, key_id, **kwargs):
        """Batch update keysets for a key."""
        url = self.client.get_key_keysets_path(self.project_name, key_id)
        patch_body = json.dumps(kwargs)
        resp, body = self.patch(url, patch_body)
        return self._parse_response(
            resp, body, self.client.get_schema().update_key_keysets
        )


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

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

    @abstractmethod
    def get_keys_path(self, project):
        """Return API endpoint path."""
        pass

    @abstractmethod
    def get_key_keysets_path(self, project, key_id):
        """Return API endpoint path for key keysets."""
        pass
