# from django.apps import AppConfig
# import os
# import json
# from django.conf import settings
# from importlib import import_module

# class PluginManagerConfig(AppConfig):
#     default_auto_field = 'django.db.models.BigAutoField'
#     name = 'apps.plugin_manager'  # 修正为当前路径

#     def ready(self):
#         try:
#             self.register_plugins()
#             self.register_templates()
#         except Exception as e:
#             print(f"Plugin initialization failed: {str(e)}")

#     def register_plugins(self):
#         plugins_dir = os.path.join(settings.BASE_DIR, 'plugins')
#         if not os.path.exists(plugins_dir):
#             return

#         for plugin_name in os.listdir(plugins_dir):
#             try:
#                 meta_path = os.path.join(plugins_dir, plugin_name,
#                                          'plugin_meta.json')
#                 if os.path.isfile(meta_path):
#                     with open(meta_path) as f:
#                         meta = json.load(f)
#                         if meta.get('enabled', False):
#                             self.safe_register_plugin(plugin_name)
#             except (json.JSONDecodeError, PermissionError) as e:
#                 print(f"Skipping plugin {plugin_name}: {str(e)}")

#     def safe_register_plugin(self, plugin_name):
#         module_path = f'plugins.{plugin_name}'
#         if module_path not in settings.INSTALLED_APPS:
#             try:
#                 import_module(module_path)
#                 settings.INSTALLED_APPS.append(module_path)
#             except ImportError:
#                 print(f"Plugin module {module_path} not found")

#     def register_templates(self):
#         templates_dir = os.path.join(settings.BASE_DIR, 'plugins/templates')
#         if os.path.isdir(templates_dir):
#             settings.TEMPLATES[0]['DIRS'].append(templates_dir)
