from typing import Type, Self, Callable

from api_graphql.abc.collection_filter_abc import CollectionFilterABC
from api_graphql.abc.field_builder_abc import FieldBuilderABC
from api_graphql.field.collection_field import CollectionField
from core.typing import T


class CollectionFieldBuilder(FieldBuilderABC):

    def __init__(self, name: str):
        FieldBuilderABC.__init__(self, name)

        self._resolver = None
        self._filter_type = None
        self._sort_type = None

    def with_resolver(self, resolver: Callable) -> Self:
        assert resolver is not None, "resolver cannot be None"
        self._resolver = resolver
        return self

    def with_filter(self, filter_type: Type[CollectionFilterABC]) -> Self:
        assert filter_type is not None, "filter cannot be None"
        self._filter_type = filter_type
        return self

    def with_sort(self, sort_type: Type[T]) -> Self:
        assert sort_type is not None, "sort cannot be None"
        self._sort_type = sort_type
        return self

    def build(self) -> CollectionField:
        assert self._resolver is not None, "resolver cannot be None"

        return CollectionField(
            self._name,
            self._require_any_permission,
            self._require_any,
            self._public,
            self._resolver,
            self._filter_type,
            self._sort_type,
        )
