/**
 * 创建待安装任务表的迁移文件
 */

'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('installation_tasks', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
        comment: '主键ID'
      },
      applicationNumber: {
        type: Sequelize.STRING(50),
        allowNull: false,
        field: 'application_number',
        comment: '申请单号'
      },
      policyNumber: {
        type: Sequelize.STRING(50),
        allowNull: false,
        field: 'policy_number',
        comment: '保单编号'
      },
      productName: {
        type: Sequelize.STRING(100),
        allowNull: false,
        field: 'product_name',
        comment: '产品名称'
      },
      customerName: {
        type: Sequelize.STRING(50),
        allowNull: false,
        field: 'customer_name',
        comment: '客户姓名'
      },
      idType: {
        type: Sequelize.ENUM('身份证', '护照', '军官证', '士兵证', '港澳台居民居住证', '其他'),
        allowNull: false,
        defaultValue: '身份证',
        field: 'id_type',
        comment: '证件类型'
      },
      idNumber: {
        type: Sequelize.STRING(30),
        allowNull: false,
        field: 'id_number',
        comment: '证件号码'
      },
      livestockSupplyType: {
        type: Sequelize.STRING(100),
        allowNull: false,
        field: 'livestock_supply_type',
        comment: '养殖生资种类'
      },
      pendingDevices: {
        type: Sequelize.TEXT,
        allowNull: true,
        field: 'pending_devices',
        comment: '待安装设备（JSON格式存储）'
      },
      installationStatus: {
        type: Sequelize.ENUM('待安装', '安装中', '已安装', '安装失败', '已取消'),
        allowNull: false,
        defaultValue: '待安装',
        field: 'installation_status',
        comment: '安装状态'
      },
      taskGeneratedTime: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
        field: 'task_generated_time',
        comment: '生成安装任务时间'
      },
      installationCompletedTime: {
        type: Sequelize.DATE,
        allowNull: true,
        field: 'installation_completed_time',
        comment: '安装完成生效时间'
      },
      priority: {
        type: Sequelize.ENUM('低', '中', '高', '紧急'),
        allowNull: false,
        defaultValue: '中',
        comment: '安装优先级'
      },
      assignedTechnician: {
        type: Sequelize.INTEGER,
        allowNull: true,
        field: 'assigned_technician',
        comment: '分配的技术员ID',
        references: {
          model: 'users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      installationAddress: {
        type: Sequelize.TEXT,
        allowNull: true,
        field: 'installation_address',
        comment: '安装地址'
      },
      contactPhone: {
        type: Sequelize.STRING(20),
        allowNull: true,
        field: 'contact_phone',
        comment: '联系电话'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注信息'
      },
      createdBy: {
        type: Sequelize.INTEGER,
        allowNull: true,
        field: 'created_by',
        comment: '创建人ID',
        references: {
          model: 'users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updatedBy: {
        type: Sequelize.INTEGER,
        allowNull: true,
        field: 'updated_by',
        comment: '更新人ID',
        references: {
          model: 'users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
        field: 'created_at',
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
        field: 'updated_at',
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    }, {
      comment: '待安装任务表'
    });

    // 创建索引
    await queryInterface.addIndex('installation_tasks', ['application_number'], {
      name: 'idx_application_number'
    });
    
    await queryInterface.addIndex('installation_tasks', ['policy_number'], {
      name: 'idx_policy_number'
    });
    
    await queryInterface.addIndex('installation_tasks', ['customer_name'], {
      name: 'idx_customer_name'
    });
    
    await queryInterface.addIndex('installation_tasks', ['installation_status'], {
      name: 'idx_installation_status'
    });
    
    await queryInterface.addIndex('installation_tasks', ['task_generated_time'], {
      name: 'idx_task_generated_time'
    });
    
    await queryInterface.addIndex('installation_tasks', ['installation_completed_time'], {
      name: 'idx_installation_completed_time'
    });
  },

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