/**
 * 创建贷款合同表迁移
 * @file 20241220000009-create-loan-contracts.js
 */
'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('bank_loan_contracts', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      contractNumber: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: '合同编号'
      },
      applicationNumber: {
        type: Sequelize.STRING(50),
        allowNull: false,
        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: '合同金额'
      },
      paidAmount: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: false,
        defaultValue: 0,
        comment: '已还款金额'
      },
      status: {
        type: Sequelize.ENUM(
          'active',
          'pending', 
          'completed',
          'defaulted',
          'cancelled'
        ),
        allowNull: false,
        defaultValue: 'pending',
        comment: '合同状态'
      },
      type: {
        type: Sequelize.ENUM(
          'livestock_collateral',
          'farmer_loan',
          'business_loan',
          'personal_loan'
        ),
        allowNull: false,
        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: '备注'
      },
      contractTime: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '合同签订时间'
      },
      disbursementTime: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '放款时间'
      },
      maturityTime: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '到期时间'
      },
      completedTime: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '完成时间'
      },
      createdBy: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '创建人ID',
        references: {
          model: 'bank_users',
          key: 'id'
        }
      },
      updatedBy: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '更新人ID',
        references: {
          model: 'bank_users',
          key: 'id'
        }
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW
      }
    });

    // 添加索引
    await queryInterface.addIndex('bank_loan_contracts', ['contractNumber']);
    await queryInterface.addIndex('bank_loan_contracts', ['applicationNumber']);
    await queryInterface.addIndex('bank_loan_contracts', ['status']);
    await queryInterface.addIndex('bank_loan_contracts', ['borrowerName']);
    await queryInterface.addIndex('bank_loan_contracts', ['farmerName']);
    await queryInterface.addIndex('bank_loan_contracts', ['contractTime']);
  },

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