from abc import abstractmethod

from api_graphql.abc.query_abc import QueryABC
from api_graphql.field.mutation_field_builder import MutationFieldBuilder
from service.permission.permissions_enum import Permissions


class MutationABC(QueryABC):
    __abstract__ = True

    @abstractmethod
    def __init__(self, name: str = __name__):
        QueryABC.__init__(self, f"{name}Mutation")

    def add_mutation_type(
        self,
        name: str,
        mutation_name: str,
        require_any_permission: list[Permissions] = None,
        public: bool = False,
    ):
        """
        Add mutation type (sub mutation) to the mutation object
        :param str name: GraphQL mutation name
        :param str mutation_name: Internal (class) mutation name without "Mutation" suffix
        :param list[Permissions] require_any_permission: List of permissions required to access the field
        :param bool public: Define if the field can resolve without authentication
        :return:
        """
        from api_graphql.definition import QUERIES

        self.field(
            MutationFieldBuilder(name)
            .with_resolver(
                lambda *args, **kwargs: [
                    x for x in QUERIES if x.name == f"{mutation_name}Mutation"
                ][0]
            )
            .with_require_any_permission(require_any_permission)
            .with_public(public)
        )
