/**
 * 审核记录模型
 * @file AuditRecord.js
 * @description 银行系统贷款申请审核记录数据模型
 */
const { DataTypes } = require('sequelize');
const BaseModel = require('./BaseModel');

class AuditRecord extends BaseModel {
  /**
   * 获取审核动作文本
   * @returns {String} 动作文本
   */
  getActionText() {
    const actionMap = {
      submit: '提交申请',
      approve: '审核通过',
      reject: '审核拒绝',
      review: '初审',
      verification: '核验',
      binding: '绑定'
    };
    return actionMap[this.action] || this.action;
  }

  /**
   * 获取审核动作颜色
   * @returns {String} 颜色
   */
  getActionColor() {
    const colorMap = {
      submit: 'blue',
      approve: 'green',
      reject: 'red',
      review: 'orange',
      verification: 'purple',
      binding: 'cyan'
    };
    return colorMap[this.action] || 'default';
  }
}

// 初始化AuditRecord模型
AuditRecord.init({
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  applicationId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '申请ID',
    references: {
      model: 'bank_loan_applications',
      key: 'id'
    }
  },
  action: {
    type: DataTypes.ENUM(
      'submit',
      'approve', 
      'reject',
      'review',
      'verification',
      'binding'
    ),
    allowNull: false,
    comment: '审核动作'
  },
  auditor: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '审核人'
  },
  auditorId: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '审核人ID'
  },
  comment: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '审核意见'
  },
  auditTime: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW,
    comment: '审核时间'
  },
  previousStatus: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '审核前状态'
  },
  newStatus: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '审核后状态'
  }
}, {
  sequelize: require('../config/database').sequelize,
  modelName: 'AuditRecord',
  tableName: 'bank_audit_records',
  timestamps: true,
  createdAt: 'createdAt',
  updatedAt: 'updatedAt'
});

module.exports = AuditRecord;
