'use strict'

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('installation_tasks', {
      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: '养殖生资种类'
      },
      equipmentToInstall: {
        type: Sequelize.DataTypes.STRING(100),
        allowNull: false,
        comment: '待安装设备'
      },
      installationStatus: {
        type: Sequelize.DataTypes.ENUM('pending', 'in-progress', 'completed', 'failed'),
        allowNull: false,
        defaultValue: 'pending',
        comment: '安装状态'
      },
      taskGenerationTime: {
        type: Sequelize.DataTypes.DATE,
        allowNull: false,
        comment: '生成安装任务时间'
      },
      completionTime: {
        type: Sequelize.DataTypes.DATE,
        allowNull: true,
        comment: '安装完成生效时间'
      },
      installationNotes: {
        type: Sequelize.DataTypes.TEXT,
        allowNull: true,
        comment: '安装备注'
      },
      installerName: {
        type: Sequelize.DataTypes.STRING(50),
        allowNull: true,
        comment: '安装员姓名'
      },
      installerPhone: {
        type: Sequelize.DataTypes.STRING(20),
        allowNull: true,
        comment: '安装员电话'
      },
      installationAddress: {
        type: Sequelize.DataTypes.STRING(200),
        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('installation_tasks', ['contractNumber'], {
      name: 'idx_installation_tasks_contract_number'
    })
    
    await queryInterface.addIndex('installation_tasks', ['installationStatus'], {
      name: 'idx_installation_tasks_status'
    })
    
    await queryInterface.addIndex('installation_tasks', ['createdBy'], {
      name: 'idx_installation_tasks_created_by'
    })
  },

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