'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('supervision_tasks', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false
      },
      applicationNumber: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: '申请单号'
      },
      contractNumber: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: '放款合同编号'
      },
      productName: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '产品名称'
      },
      customerName: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '客户姓名'
      },
      idType: {
        type: Sequelize.ENUM('id_card', 'passport', 'other'),
        allowNull: false,
        defaultValue: 'id_card',
        comment: '证件类型：id_card-身份证，passport-护照，other-其他'
      },
      idNumber: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '证件号码'
      },
      assetType: {
        type: Sequelize.ENUM('cattle', 'sheep', 'pig', 'poultry', 'other'),
        allowNull: false,
        defaultValue: 'cattle',
        comment: '养殖生资种类：cattle-牛，sheep-羊，pig-猪，poultry-家禽，other-其他'
      },
      assetQuantity: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '监管生资数量'
      },
      supervisionStatus: {
        type: Sequelize.ENUM('pending', 'supervising', 'completed', 'suspended'),
        allowNull: false,
        defaultValue: 'pending',
        comment: '监管状态：pending-待监管，supervising-监管中，completed-已完成，suspended-已暂停'
      },
      importTime: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
        comment: '任务导入时间'
      },
      startTime: {
        type: Sequelize.DATEONLY,
        allowNull: false,
        comment: '监管起始时间'
      },
      endTime: {
        type: Sequelize.DATEONLY,
        allowNull: false,
        comment: '监管结束时间'
      },
      loanAmount: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: true,
        defaultValue: 0.00,
        comment: '贷款金额'
      },
      interestRate: {
        type: Sequelize.DECIMAL(5, 4),
        allowNull: true,
        defaultValue: 0.0000,
        comment: '利率'
      },
      loanTerm: {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: 12,
        comment: '贷款期限（月）'
      },
      supervisorName: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '监管员姓名'
      },
      supervisorPhone: {
        type: Sequelize.STRING(20),
        allowNull: true,
        comment: '监管员电话'
      },
      farmAddress: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: '养殖场地址'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注'
      },
      createdBy: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '创建人ID'
      },
      updatedBy: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '更新人ID'
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    // 添加索引
    await queryInterface.addIndex('supervision_tasks', ['applicationNumber']);
    await queryInterface.addIndex('supervision_tasks', ['contractNumber']);
    await queryInterface.addIndex('supervision_tasks', ['customerName']);
    await queryInterface.addIndex('supervision_tasks', ['supervisionStatus']);
    await queryInterface.addIndex('supervision_tasks', ['createdBy']);
    await queryInterface.addIndex('supervision_tasks', ['startTime']);
    await queryInterface.addIndex('supervision_tasks', ['endTime']);
  },

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