from django.db import models
import json
from pathlib import Path
from django.conf import settings

plugins_dir = Path(settings.PLUGINS_DIR)


# 虚拟模型类（不创建数据库表）
class Plugin(models.Model):
    # 定义你的字段（这些字段不会在数据库中创建）
    name = models.CharField('名称', max_length=100)
    version = models.CharField('版本', max_length=20)
    enabled = models.BooleanField('启用状态', default=True)

    class Meta:
        managed = False  # 告诉Django不要管理数据库表
        verbose_name = '插件'
        verbose_name_plural = '插件管理'

    @classmethod
    def get_plugins(cls):
        # 原有方法实现不变
        plugins = []
        for plugin_dir in plugins_dir.iterdir():
            if not plugin_dir.is_dir():
                continue
            meta_file = plugin_dir / 'plugin_meta.json'
            if not meta_file.exists():
                continue
            with open(meta_file, encoding='utf-8') as f:
                data = json.load(f)
            plugins.append({
                'package': plugin_dir.name,
                'meta': data,
                'has_image': (plugin_dir / 'plugin.jpg').exists()
            })
        return plugins
