const Sequelize = require('sequelize');

/**
 * 意见反馈表模型
 * 记录用户的意见反馈信息
 */
module.exports = (db) => {
  const feedback = db.define('feedback', {
    // 用户信息
    user_id: {
      comment: '用户ID（关联 pla_account.id）',
      type: Sequelize.INTEGER,
      allowNull: false,
      references: {
        model: 'pla_account',
        key: 'id'
      }
    },
    sn_code: {
      comment: '设备SN码',
      type: Sequelize.STRING(50),
      allowNull: false,
      defaultValue: ''
    },
    
    // 反馈信息
    type: {
      comment: '反馈类型: bug-Bug反馈, suggestion-功能建议, question-使用问题, other-其他',
      type: Sequelize.STRING(20),
      allowNull: false,
      defaultValue: 'other'
    },
    content: {
      comment: '反馈内容',
      type: Sequelize.TEXT,
      allowNull: false
    },
    contact: {
      comment: '联系方式（手机号、邮箱等）',
      type: Sequelize.STRING(100),
      allowNull: true,
      defaultValue: ''
    },
    
    // 处理状态
    status: {
      comment: '处理状态: pending-待处理, processing-处理中, completed-已完成, rejected-已拒绝',
      type: Sequelize.STRING(20),
      allowNull: false,
      defaultValue: 'pending'
    },
    reply_content: {
      comment: '回复内容',
      type: Sequelize.TEXT,
      allowNull: true
    },
    reply_time: {
      comment: '回复时间',
      type: Sequelize.DATE,
      allowNull: true
    },
    reply_user_id: {
      comment: '回复人ID（管理员ID）',
      type: Sequelize.INTEGER,
      allowNull: true
    },
    
    // 备注
    remark: {
      comment: '备注',
      type: Sequelize.TEXT,
      allowNull: true
    },
    
    // 软删除
    is_delete: {
      comment: '是否删除（0-未删除，1-已删除）',
      type: Sequelize.TINYINT(1),
      allowNull: false,
      defaultValue: 0
    }
  }, {
    tableName: 'feedback',
    indexes: [
      {
        name: 'idx_user_id',
        fields: ['user_id']
      },
      {
        name: 'idx_sn_code',
        fields: ['sn_code']
      },
      {
        name: 'idx_type',
        fields: ['type']
      },
      {
        name: 'idx_status',
        fields: ['status']
      },
      {
        name: 'idx_create_time',
        fields: ['create_time']
      }
    ]
  });

  // feedback.sync({ alter: true });

  return feedback;
};

