from abc import ABC
from datetime import datetime
from typing import Optional, Union, Type


from core.typing import AttributeFilter


class FilterABC[T](ABC):
    def __init__(
        self,
        obj: Optional[dict],
    ):
        ABC.__init__(self)
        self._obj = obj

        self._values = {}

    def add_field(
        self,
        field: str,
        filter_type: Union[
            Type["FilterABC"], Type[Union[int, str, bool, datetime, list]]
        ],
        db_name=None,
    ):
        if field not in self._obj:
            return

        if db_name is None:
            db_name = field

        if issubclass(filter_type, FilterABC):
            f = filter_type(self._obj[field])
            self._values[db_name] = f.to_attribute_filter()
            return

        self._values[db_name] = filter_type(self._obj[field])

    def to_attribute_filter(self) -> AttributeFilter:
        return self._values
