# Copyright (C) 2022-2023 HERE Global B.V. and its affiliate(s).
# All rights reserved.
#
# This software and other materials contain proprietary information
# controlled by HERE and are protected by applicable copyright legislation.
# Any use and utilization of this software and other materials and
# disclosure to any third parties is conditional upon having a separate
# agreement with HERE for the access, use, utilization or disclosure of this
# software. In the absence of such agreement, the use of the software is not
# allowed.

"""HERE platform Schema abstraction."""

from typing import TYPE_CHECKING, Optional

from here.platform.api.artifact_api import ArtifactApi
from here.platform.utils import JsonDictDocument

if TYPE_CHECKING:
    from here.platform.platform import Platform


class SchemaConfiguration(JsonDictDocument):
    """
    A JSON document for schema configuration used in APIs loaded into a ``Dict``.
    """

    @property
    def group_id(self) -> Optional[str]:
        """Group ID in the schema response"""
        return str(self.json["groupId"]) if "groupId" in self.json else None

    @property
    def artifact_id(self) -> Optional[str]:
        """Artifact ID in the schema response"""
        return str(self.json["artifactId"]) if "artifactId" in self.json else None

    @property
    def version(self) -> Optional[str]:
        """Version in the schema response"""
        return str(self.json["version"]) if "version" in self.json else None

    @property
    def created(self) -> Optional[str]:
        """Created date in the schema response"""
        return str(self.json["created"]) if "created" in self.json else None

    @property
    def updated(self) -> Optional[str]:
        """Updated date in the schema response"""
        return str(self.json["updated"]) if "updated" in self.json else None

    @property
    def name(self) -> Optional[str]:
        """Name in the schema response"""
        return str(self.json["name"]) if "name" in self.json else None

    @property
    def summary(self) -> Optional[str]:
        """Summary in the schema response"""
        return str(self.json["summary"]) if "summary" in self.json else None

    @property
    def type(self) -> Optional[str]:
        """Type in the schema response"""
        return str(self.json["type"]) if "type" in self.json else None


class Schema:
    """
    HERE platform Schema abstraction.
    """

    def __init__(
        self,
        hrn: str,
        configuration: SchemaConfiguration,
        platform: "Platform",
    ):
        """
        Initiate a new schema object with the given configuration.

        :param configuration: an instance of SchemaConfiguration
        :param hrn: hrn of the artifact
        :param platform: platform object
        """
        self.hrn: str = hrn
        self.platform = platform
        self.group_id = configuration.group_id
        self.artifact_id = configuration.artifact_id
        self.version = configuration.version
        self.created = configuration.created
        self.updated = configuration.updated
        self.name = configuration.name
        self.summary = configuration.summary
        self.type = configuration.type
        self.artifact_api: ArtifactApi = platform.artifact_api

    def get_document(self, filename) -> str:
        """
        Return the url to schema documentation.

        :param filename: The path to schema file.
        :return: response from the API.
        """
        return self.artifact_api.get_schema_document(schema_hrn=self.hrn, file=filename)

    def update_permission(self, body: dict) -> bool:
        """
        Grant/revoke permissions for schema. READ grants 'readResource',
        MODIFY grants 'modifyResource', SHARE grants 'shareResource'.

        :param body: a dictionary with permissions.
        :return: response from the API.
        """

        return self.artifact_api.update_schema_permission(schema_hrn=self.hrn, body=body)
