/**
 * 创建贷款申请表迁移
 * @file 20241220000007-create-loan-applications.js
 */
'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('bank_loan_applications', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      applicationNumber: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: '申请单号'
      },
      productName: {
        type: Sequelize.STRING(200),
        allowNull: false,
        comment: '贷款产品名称'
      },
      farmerName: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '申请养殖户姓名'
      },
      borrowerName: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '贷款人姓名'
      },
      borrowerIdNumber: {
        type: Sequelize.STRING(20),
        allowNull: false,
        comment: '贷款人身份证号'
      },
      assetType: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '生资种类'
      },
      applicationQuantity: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '申请数量'
      },
      amount: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: false,
        comment: '申请额度'
      },
      status: {
        type: Sequelize.ENUM(
          'pending_review',
          'verification_pending', 
          'pending_binding',
          'approved',
          'rejected'
        ),
        allowNull: false,
        defaultValue: 'pending_review',
        comment: '申请状态'
      },
      type: {
        type: Sequelize.ENUM('personal', 'business', 'mortgage'),
        allowNull: false,
        defaultValue: 'personal',
        comment: '申请类型'
      },
      term: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '申请期限（月）'
      },
      interestRate: {
        type: Sequelize.DECIMAL(5, 2),
        allowNull: false,
        comment: '预计利率'
      },
      phone: {
        type: Sequelize.STRING(20),
        allowNull: false,
        comment: '联系电话'
      },
      purpose: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '申请用途'
      },
      remark: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注'
      },
      applicationTime: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '申请时间'
      },
      approvedTime: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '审批通过时间'
      },
      rejectedTime: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '审批拒绝时间'
      },
      applicantId: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '申请人ID',
        references: {
          model: 'bank_users',
          key: 'id'
        }
      },
      approvedBy: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '审批人ID',
        references: {
          model: 'bank_users',
          key: 'id'
        }
      },
      rejectedBy: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '拒绝人ID',
        references: {
          model: 'bank_users',
          key: 'id'
        }
      },
      rejectionReason: {
        type: Sequelize.TEXT,
        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_loan_applications', ['applicationNumber']);
    await queryInterface.addIndex('bank_loan_applications', ['status']);
    await queryInterface.addIndex('bank_loan_applications', ['borrowerName']);
    await queryInterface.addIndex('bank_loan_applications', ['farmerName']);
    await queryInterface.addIndex('bank_loan_applications', ['applicationTime']);
  },

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