from enum import Enum

from .grpc.search_pb2 import (
    CreateIndexedContentRequest,
    SearchIndexedContentRequest,
)
from .grpc.search_pb2_grpc import (
    CreateIndexedContentControllerStub,
    SearchIndexedContentControllerStub,
)

SUCCESS_CODE = "0"


def _singletonize_class_vars(cls):
    for attr_name, attr_value in cls.__dict__.items():
        if isinstance(attr_value, type):  # it's a class
            orig_cls = attr_value

            class SingletonWrapper(orig_cls):
                _instances = {}

                def __new__(cls, *args, **kwargs):
                    if cls not in cls._instances:
                        cls._instances[cls] = super().__new__(cls)
                    return cls._instances[cls]

            setattr(cls, attr_name, SingletonWrapper)
    return cls


class RequestMessages:
    CREATE = CreateIndexedContentRequest
    SEARCH = SearchIndexedContentRequest


class RPCMethodNames:
    CREATE = "Create"
    SEARCH = "Search"


@_singletonize_class_vars
class Stubs:
    CREATE = CreateIndexedContentControllerStub
    SEARCH = SearchIndexedContentControllerStub


class ItemTypes(str, Enum):
    BLOG = "blog"
    COURSE = "course"
    MASTER_COURSE = "master_course"
    LESSON = "lesson"
