/**
 * 贷款解押历史记录模型
 * @file LoanReleaseHistory.js
 * @description 银行系统贷款解押历史记录数据模型
 */
const { DataTypes } = require('sequelize');
const BaseModel = require('./BaseModel');

class LoanReleaseHistory extends BaseModel {
  /**
   * 获取操作动作文本
   * @returns {String} 动作文本
   */
  getActionText() {
    const actionMap = {
      apply: '提交申请',
      process: '开始处理',
      approve: '审核通过',
      reject: '审核拒绝',
      complete: '处理完成',
      release: '解押完成'
    };
    return actionMap[this.action] || this.action;
  }

  /**
   * 获取操作动作颜色
   * @returns {String} 颜色
   */
  getActionColor() {
    const colorMap = {
      apply: 'blue',
      process: 'orange',
      approve: 'green',
      reject: 'red',
      complete: 'green',
      release: 'default'
    };
    return colorMap[this.action] || 'default';
  }
}

// 初始化LoanReleaseHistory模型
LoanReleaseHistory.init({
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  release_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '解押申请ID'
  },
  action: {
    type: DataTypes.ENUM(
      'apply',
      'process',
      'approve',
      'reject',
      'complete',
      'release'
    ),
    allowNull: false,
    comment: '操作动作'
  },
  operator: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '操作人'
  },
  operator_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '操作人ID'
  },
  comment: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '操作备注'
  },
  previous_status: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '操作前状态'
  },
  new_status: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '操作后状态'
  },
  operation_time: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW,
    comment: '操作时间'
  }
}, {
  sequelize: require('../config/database').sequelize,
  modelName: 'LoanReleaseHistory',
  tableName: 'bank_loan_release_histories',
  timestamps: true,
  underscored: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

module.exports = LoanReleaseHistory;
