from django.apps import apps
from django.db import connection


class PluginLoader:

    @classmethod
    def validate_databases(cls):
        missing_tables = []
        for app_config in apps.get_app_configs():
            if app_config.name.startswith('plugins.'):
                required_tables = [f"{app_config.label}_config"]
                existing_tables = connection.introspection.table_names()
                if not all(t in existing_tables for t in required_tables):
                    missing_tables.append(app_config.label)
        return missing_tables

    @classmethod
    def initialize_plugins(cls):
        from django.core.management import call_command
        if missing := cls.validate_databases():
            call_command('auto_migrate')
