# Path: django_cassandra_engine/utils.py
# def get_cql_models(app, connection=None, keyspace=None):
#     """
#     :param app: django models module
#     :param connection: connection name
#     :param keyspace: keyspace
#     :return: list of all cassandra.cqlengine.Model within app that should be
#     synced to keyspace.
#     """
#     from .models import DjangoCassandraModel
# 
#     models = []
#     single_cassandra_connection = len(list(get_cassandra_connections())) == 1
#     is_default_connection = (
#         connection == DEFAULT_DB_ALIAS or single_cassandra_connection
#     )
# 
#     for name, obj in inspect.getmembers(app):
#         cql_model_types = (cqlengine.models.Model, DjangoCassandraModel)
#         if (
#             inspect.isclass(obj)
#             and issubclass(obj, cql_model_types)
#             and not obj.__abstract__
#         ):
#             if (
#                 obj.__connection__ == connection
#                 or (obj.__connection__ is None and is_default_connection)
#                 or obj.__connection__ is None
#                 and obj.__keyspace__ is not None
#                 and obj.__keyspace__ == keyspace
#             ):
#                 models.append(obj)
# 
#     return models
# 
# def get_installed_apps():
#     """Return list of all installed apps"""
#     from django.apps import apps
# 
#     return [
#         a.models_module for a in apps.get_app_configs() if a.models_module is not None
#     ]


from itertools import chain
from django.db.backends.base.introspection import BaseDatabaseIntrospection
from django_cassandra_engine.utils import get_cql_models, get_installed_apps




class CassandraDatabaseIntrospection(BaseDatabaseIntrospection):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._cql_models = {}
        self._models_discovered = False

    def _discover_models(self):
        """
        Return a dict containing a list of cassandra.cqlengine.Model classes
        within installed App.
        """
        apps = get_installed_apps()
        connection = self.connection.connection.alias
        keyspace = self.connection.connection.keyspace

        for app in apps:

# Path: django_cassandra_engine/utils.py
# def get_cql_models(app, connection=None, keyspace=None):
#     """
#     :param app: django models module
#     :param connection: connection name
#     :param keyspace: keyspace
#     :return: list of all cassandra.cqlengine.Model within app that should be
#     synced to keyspace.
#     """
#     from .models import DjangoCassandraModel
# 
#     models = []
#     single_cassandra_connection = len(list(get_cassandra_connections())) == 1
#     is_default_connection = (
#         connection == DEFAULT_DB_ALIAS or single_cassandra_connection
#     )
# 
#     for name, obj in inspect.getmembers(app):
#         cql_model_types = (cqlengine.models.Model, DjangoCassandraModel)
#         if (
#             inspect.isclass(obj)
#             and issubclass(obj, cql_model_types)
#             and not obj.__abstract__
#         ):
#             if (
#                 obj.__connection__ == connection
#                 or (obj.__connection__ is None and is_default_connection)
#                 or obj.__connection__ is None
#                 and obj.__keyspace__ is not None
#                 and obj.__keyspace__ == keyspace
#             ):
#                 models.append(obj)
# 
#     return models
# 
# def get_installed_apps():
#     """Return list of all installed apps"""
#     from django.apps import apps
# 
#     return [
#         a.models_module for a in apps.get_app_configs() if a.models_module is not None
#     ]


# Path: django_cassandra_engine/base/introspection.py
from itertools import chain
from django.db.backends.base.introspection import BaseDatabaseIntrospection
from django_cassandra_engine.utils import get_cql_models, get_installed_apps





class CassandraDatabaseIntrospection(BaseDatabaseIntrospection):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._cql_models = {}
        self._models_discovered = False

    def _discover_models(self):
        """
        Return a dict containing a list of cassandra.cqlengine.Model classes
        within installed App.
        """
        apps = get_installed_apps()
        connection = self.connection.connection.alias
        keyspace = self.connection.connection.keyspace

        for app in apps:

            self._cql_models[app.__name__] = get_cql_models(