/**
 * 创建审核记录表迁移
 * @file 20241220000008-create-audit-records.js
 */
'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('bank_audit_records', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      applicationId: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '申请ID',
        references: {
          model: 'bank_loan_applications',
          key: 'id'
        }
      },
      action: {
        type: Sequelize.ENUM(
          'submit',
          'approve', 
          'reject',
          'review',
          'verification',
          'binding'
        ),
        allowNull: false,
        comment: '审核动作'
      },
      auditor: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '审核人'
      },
      auditorId: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '审核人ID',
        references: {
          model: 'bank_users',
          key: 'id'
        }
      },
      comment: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '审核意见'
      },
      auditTime: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '审核时间'
      },
      previousStatus: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '审核前状态'
      },
      newStatus: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '审核后状态'
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW
      }
    });

    // 添加索引
    await queryInterface.addIndex('bank_audit_records', ['applicationId']);
    await queryInterface.addIndex('bank_audit_records', ['action']);
    await queryInterface.addIndex('bank_audit_records', ['auditorId']);
    await queryInterface.addIndex('bank_audit_records', ['auditTime']);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('bank_audit_records');
  }
};
