# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

from abc import ABC, abstractmethod
from enum import Enum
from typing import Optional


class MetadataObject(ABC):
    """The MetadataObject is the basic unit of the Gravitino system. It
    represents the metadata object in the Apache Gravitino system. The object
    can be a metalake, catalog, schema, table, topic, etc.
    """

    class Type(Enum):
        """The type of object in the Gravitino system. Every type will map one
        kind of the entity of the underlying system.
        """

        METALAKE = "metalake"
        """A metalake is a concept of tenant. It means an organization. A metalake
        contains many data sources.
        """

        CATALOG = "catalog"
        """A catalog is a collection of metadata from a specific metadata source,
        like Apache Hive catalog, Apache Iceberg catalog, JDBC catalog, etc.
        """

        SCHEMA = "schema"
        """"A schema is a sub collection of the catalog. The schema can contain filesets,
        tables, topics, etc.
        """

        FILESET = "fileset"
        """A fileset is mapped to a directory on a file system like HDFS, S3, ADLS,
        GCS, etc.
        """

        TABLE = "table"
        """A table is mapped the table of relational data sources like Apache Hive,
        MySQL, etc.
        """

        TOPIC = "topic"
        """A topic is mapped the topic of messaging data sources like Apache Kafka,
        Apache Pulsar, etc.
        """

        COLUMN = "column"
        """A column is a sub-collection of the table that represents a group of
        same type data.
        """

        ROLE = "role"
        """A role is an object contains specific securable objects with privileges."""

        MODEL = "model"
        """A model is mapped to the model artifact in ML."""

    @abstractmethod
    def parent(self) -> Optional[str]:
        """The parent full name of the object.

        If the object doesn't have parent, this method will return `None`.

        Returns:
            Optional[str]: The parent full name of the object.
        """

    @abstractmethod
    def type(self) -> Type:
        """The type of the object.

        Returns:
            Type: The type of the object.
        """

    @abstractmethod
    def name(self) -> str:
        """The name of the object.

        Returns:
            str: The name of the object.
        """

    def full_name(self) -> str:
        """The full name of the object.

        Full name will be separated by "." to represent a string identifier of the object,
        like catalog, catalog.table, etc.

        Returns:
            str: The name of the object.
        """

        parent = self.parent()
        return ".".join([parent, self.name()]) if parent else self.name()
