'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('loan_products', {
      id: {
        type: Sequelize.DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comment: '主键ID'
      },
      productName: {
        type: Sequelize.DataTypes.STRING(200),
        allowNull: false,
        comment: '贷款产品名称'
      },
      loanAmount: {
        type: Sequelize.DataTypes.STRING(100),
        allowNull: false,
        comment: '贷款额度'
      },
      loanTerm: {
        type: Sequelize.DataTypes.INTEGER,
        allowNull: false,
        comment: '贷款周期（月）'
      },
      interestRate: {
        type: Sequelize.DataTypes.DECIMAL(5, 2),
        allowNull: false,
        comment: '贷款利率（%）'
      },
      serviceArea: {
        type: Sequelize.DataTypes.STRING(200),
        allowNull: false,
        comment: '服务区域'
      },
      servicePhone: {
        type: Sequelize.DataTypes.STRING(20),
        allowNull: false,
        comment: '服务电话'
      },
      totalCustomers: {
        type: Sequelize.DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '服务客户总数量'
      },
      supervisionCustomers: {
        type: Sequelize.DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '监管中客户数量'
      },
      completedCustomers: {
        type: Sequelize.DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '已结项客户数量'
      },
      onSaleStatus: {
        type: Sequelize.DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: '在售状态'
      },
      productDescription: {
        type: Sequelize.DataTypes.TEXT,
        allowNull: true,
        comment: '产品描述'
      },
      applicationRequirements: {
        type: Sequelize.DataTypes.TEXT,
        allowNull: true,
        comment: '申请条件'
      },
      requiredDocuments: {
        type: Sequelize.DataTypes.TEXT,
        allowNull: true,
        comment: '所需材料'
      },
      approvalProcess: {
        type: Sequelize.DataTypes.TEXT,
        allowNull: true,
        comment: '审批流程'
      },
      riskLevel: {
        type: Sequelize.DataTypes.ENUM('LOW', 'MEDIUM', 'HIGH'),
        allowNull: false,
        defaultValue: 'MEDIUM',
        comment: '风险等级'
      },
      minLoanAmount: {
        type: Sequelize.DataTypes.DECIMAL(15, 2),
        allowNull: true,
        comment: '最小贷款金额'
      },
      maxLoanAmount: {
        type: Sequelize.DataTypes.DECIMAL(15, 2),
        allowNull: true,
        comment: '最大贷款金额'
      },
      createdBy: {
        type: Sequelize.DataTypes.INTEGER,
        allowNull: false,
        comment: '创建人ID'
      },
      updatedBy: {
        type: Sequelize.DataTypes.INTEGER,
        allowNull: true,
        comment: '更新人ID'
      },
      createdAt: {
        type: Sequelize.DataTypes.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
        comment: '创建时间'
      },
      updatedAt: {
        type: Sequelize.DataTypes.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
        comment: '更新时间'
      }
    }, {
      comment: '贷款商品表',
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci'
    });

    // 添加索引
    await queryInterface.addIndex('loan_products', ['productName'], {
      name: 'idx_loan_products_product_name'
    });
    
    await queryInterface.addIndex('loan_products', ['onSaleStatus'], {
      name: 'idx_loan_products_on_sale_status'
    });
    
    await queryInterface.addIndex('loan_products', ['createdBy'], {
      name: 'idx_loan_products_created_by'
    });
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('loan_products');
  }
};
