const Sequelize = require('sequelize');

/**
 * 系统配置表模型
 * 存储系统的各种配置参数
 */
module.exports = (db) => {
 const system_config=  db.define("system_config", {
    // 配置基本信息
    configKey: {
      comment: '配置键(唯一)',
      type: Sequelize.STRING(100),
      allowNull: false,
      unique: true
    },
    configValue: {
      comment: '配置值',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    configType: {
      comment: '配置类型: string-字符串, number-数字, boolean-布尔, json-JSON对象',
      type: Sequelize.STRING(20),
      allowNull: false,
      defaultValue: 'string'
    },
    
    // 分类信息
    category: {
      comment: '配置分类: system-系统, ai-AI服务, mqtt-MQTT, schedule-调度, platform-平台',
      type: Sequelize.STRING(50),
      allowNull: false,
      defaultValue: 'system'
    },
    
    // 描述信息
    configName: {
      comment: '配置名称',
      type: Sequelize.STRING(200),
      allowNull: true,
      defaultValue: ''
    },
    description: {
      comment: '配置描述',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    
    // 默认值
    defaultValue: {
      comment: '默认值',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    
    // 验证规则
    validationRule: {
      comment: '验证规则(JSON)',
      type: Sequelize.TEXT,
      allowNull: true,
      defaultValue: ''
    },
    
    // 状态信息
    isActive: {
      comment: '是否启用',
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    isSystem: {
      comment: '是否系统配置(系统配置不可删除)',
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    isEncrypted: {
      comment: '是否加密存储',
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    
    // 排序和显示
    sortOrder: {
      comment: '排序顺序',
      type: Sequelize.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    isVisible: {
      comment: '是否在界面显示',
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    
    
  }, {
    timestamps: false,
    indexes: [
      {
        unique: true,
        fields: ['configKey']
      },
      {
        unique: false,
        fields: ['category']
      },
      {
        unique: false,
        fields: ['isActive']
      },
      {
        unique: false,
        fields: ['sortOrder']
      }
    ]
  });

    // system_config.sync({ force: true });

  return system_config

};

