'use strict'

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('completed_supervisions', {
      id: {
        type: Sequelize.DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comment: '主键ID'
      },
      applicationNumber: {
        type: Sequelize.DataTypes.STRING(50),
        allowNull: false,
        unique: true,
        comment: '申请单号'
      },
      contractNumber: {
        type: Sequelize.DataTypes.STRING(50),
        allowNull: false,
        comment: '放款合同编号'
      },
      productName: {
        type: Sequelize.DataTypes.STRING(100),
        allowNull: false,
        comment: '产品名称'
      },
      customerName: {
        type: Sequelize.DataTypes.STRING(50),
        allowNull: false,
        comment: '客户姓名'
      },
      idType: {
        type: Sequelize.DataTypes.ENUM('ID_CARD', 'PASSPORT', 'OTHER'),
        allowNull: false,
        defaultValue: 'ID_CARD',
        comment: '证件类型'
      },
      idNumber: {
        type: Sequelize.DataTypes.STRING(50),
        allowNull: false,
        comment: '证件号码'
      },
      assetType: {
        type: Sequelize.DataTypes.STRING(50),
        allowNull: false,
        comment: '养殖生资种类'
      },
      assetQuantity: {
        type: Sequelize.DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '监管生资数量'
      },
      totalRepaymentPeriods: {
        type: Sequelize.DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '总还款期数'
      },
      settlementStatus: {
        type: Sequelize.DataTypes.ENUM('settled', 'unsettled', 'partial'),
        allowNull: false,
        defaultValue: 'unsettled',
        comment: '结清状态'
      },
      settlementDate: {
        type: Sequelize.DataTypes.DATEONLY,
        allowNull: true,
        comment: '结清日期'
      },
      importTime: {
        type: Sequelize.DataTypes.DATE,
        allowNull: false,
        comment: '结清任务导入时间'
      },
      settlementAmount: {
        type: Sequelize.DataTypes.DECIMAL(15, 2),
        allowNull: true,
        comment: '结清金额'
      },
      remainingAmount: {
        type: Sequelize.DataTypes.DECIMAL(15, 2),
        allowNull: true,
        comment: '剩余金额'
      },
      settlementNotes: {
        type: Sequelize.DataTypes.TEXT,
        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: '监管任务已结项表'
    })

    // 添加索引
    await queryInterface.addIndex('completed_supervisions', ['contractNumber'], {
      name: 'idx_completed_supervisions_contract_number'
    })
    
    await queryInterface.addIndex('completed_supervisions', ['settlementStatus'], {
      name: 'idx_completed_supervisions_settlement_status'
    })
    
    await queryInterface.addIndex('completed_supervisions', ['createdBy'], {
      name: 'idx_completed_supervisions_created_by'
    })
  },

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