/**
 * 创建监管任务表的迁移文件
 */

'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('supervisory_tasks', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
        comment: '主键ID'
      },
      applicationNumber: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        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: '产品名称'
      },
      insurancePeriod: {
        type: Sequelize.STRING(50),
        allowNull: false,
        field: 'insurance_period',
        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: '证件号码'
      },
      applicableSupplies: {
        type: Sequelize.TEXT,
        allowNull: true,
        field: 'applicable_supplies',
        comment: '适用生资（JSON格式存储）'
      },
      supervisorySuppliesQuantity: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        field: 'supervisory_supplies_quantity',
        comment: '监管生资数量'
      },
      taskStatus: {
        type: Sequelize.ENUM('待处理', '处理中', '已完成', '已取消'),
        allowNull: false,
        defaultValue: '待处理',
        field: 'task_status',
        comment: '任务状态'
      },
      priority: {
        type: Sequelize.ENUM('低', '中', '高', '紧急'),
        allowNull: false,
        defaultValue: '中',
        comment: '任务优先级'
      },
      assignedTo: {
        type: Sequelize.INTEGER,
        allowNull: true,
        field: 'assigned_to',
        comment: '分配给（用户ID）',
        references: {
          model: 'users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      dueDate: {
        type: Sequelize.DATE,
        allowNull: true,
        field: 'due_date',
        comment: '截止日期'
      },
      completedAt: {
        type: Sequelize.DATE,
        allowNull: true,
        field: 'completed_at',
        comment: '完成时间'
      },
      notes: {
        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('supervisory_tasks', ['application_number'], {
      name: 'idx_application_number'
    });
    
    await queryInterface.addIndex('supervisory_tasks', ['policy_number'], {
      name: 'idx_policy_number'
    });
    
    await queryInterface.addIndex('supervisory_tasks', ['customer_name'], {
      name: 'idx_customer_name'
    });
    
    await queryInterface.addIndex('supervisory_tasks', ['task_status'], {
      name: 'idx_task_status'
    });
    
    await queryInterface.addIndex('supervisory_tasks', ['created_at'], {
      name: 'idx_created_at'
    });
  },

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