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

class LoanApplication extends BaseModel {
  /**
   * 获取申请状态文本
   * @returns {String} 状态文本
   */
  getStatusText() {
    const statusMap = {
      pending: '待审核',
      approved: '已通过',
      rejected: '已拒绝',
      completed: '已完成'
    };
    return statusMap[this.status] || this.status;
  }

  /**
   * 格式化申请金额
   * @returns {String} 格式化后的金额
   */
  getFormattedAmount() {
    return `${this.loan_amount.toFixed(2)}元`;
  }
}

// 初始化LoanApplication模型
LoanApplication.init({
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  customer_name: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '客户姓名'
  },
  customer_phone: {
    type: DataTypes.STRING(20),
    allowNull: false,
    comment: '客户电话'
  },
  customer_id_card: {
    type: DataTypes.STRING(18),
    allowNull: false,
    comment: '客户身份证号'
  },
  loan_amount: {
    type: DataTypes.DECIMAL(15, 2),
    allowNull: false,
    comment: '贷款金额'
  },
  loan_term: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '贷款期限（月）'
  },
  interest_rate: {
    type: DataTypes.DECIMAL(5, 2),
    allowNull: false,
    comment: '贷款利率（%）'
  },
  application_date: {
    type: DataTypes.DATEONLY,
    allowNull: false,
    comment: '申请日期'
  },
  status: {
    type: DataTypes.ENUM('pending', 'approved', 'rejected', 'completed'),
    allowNull: true,
    defaultValue: 'pending',
    comment: '申请状态'
  }
}, {
  sequelize: require('../config/database').sequelize,
  modelName: 'LoanApplication',
  tableName: 'loan_applications',
  timestamps: true,
  underscored: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

module.exports = LoanApplication;
