from django.apps import AppConfig
from django.conf import settings
from django.utils.module_loading import autodiscover_modules

from . import backend
from .backends.elasticsearch import ElasticsearchBackend
from .backends.postgres import PostgresBackend

class SearchConfig(AppConfig):
    name = ""
    verbose_name = "Search"

    def ready(self):
        This magic executes modules named search_indexes in every installed app. Search indexes
        is registered this way.
        if not settings.TESTING:
            # Simple way to initialize the search backend. We may change this in the future.
            if settings.SEARCH_BACKEND == "elasticsearch":
                search_backed = ElasticsearchBackend()
            elif settings.SEARCH_BACKEND == "postgres":
                search_backed = PostgresBackend()
                raise ValueError("Invalid search backend")

            backend.current_backend = search_backed

            from .signals import post_save_callback, post_delete_callback  # noqa