const { DataTypes } = require('sequelize');

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('supervision_tasks', {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comment: '监管任务ID'
      },
      applicationId: {
        type: DataTypes.STRING(50),
        allowNull: false,
        comment: '申请单号',
        field: 'application_id'
      },
      policyId: {
        type: DataTypes.STRING(50),
        allowNull: false,
        comment: '保单编号',
        field: 'policy_id'
      },
      productName: {
        type: DataTypes.STRING(100),
        allowNull: false,
        comment: '产品名称',
        field: 'product_name'
      },
      insurancePeriod: {
        type: DataTypes.STRING(100),
        allowNull: false,
        comment: '保险期间',
        field: 'insurance_period'
      },
      customerName: {
        type: DataTypes.STRING(50),
        allowNull: false,
        comment: '客户姓名',
        field: 'customer_name'
      },
      documentType: {
        type: DataTypes.STRING(20),
        allowNull: false,
        comment: '证件类型',
        field: 'document_type'
      },
      documentNumber: {
        type: DataTypes.STRING(50),
        allowNull: false,
        comment: '证件号码',
        field: 'document_number'
      },
      applicableAmount: {
        type: DataTypes.DECIMAL(15, 2),
        allowNull: false,
        comment: '适用金额',
        field: 'applicable_amount'
      },
      supervisionDataCount: {
        type: DataTypes.INTEGER,
        defaultValue: 0,
        comment: '监管生成数量',
        field: 'supervision_data_count'
      },
      status: {
        type: DataTypes.ENUM('pending', 'processing', 'completed', 'rejected'),
        defaultValue: 'pending',
        comment: '状态: pending-待处理, processing-处理中, completed-已完成, rejected-已拒绝'
      },
      taskType: {
        type: DataTypes.ENUM('new_application', 'task_guidance', 'batch_operation'),
        allowNull: false,
        comment: '任务类型: new_application-新增监管任务, task_guidance-任务导入, batch_operation-批量新增',
        field: 'task_type'
      },
      assignedTo: {
        type: DataTypes.INTEGER,
        allowNull: true,
        comment: '分配给用户ID',
        field: 'assigned_to',
        references: {
          model: 'users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      priority: {
        type: DataTypes.ENUM('low', 'medium', 'high', 'urgent'),
        defaultValue: 'medium',
        comment: '优先级'
      },
      dueDate: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: '截止日期',
        field: 'due_date'
      },
      completedAt: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: '完成时间',
        field: 'completed_at'
      },
      remarks: {
        type: DataTypes.TEXT,
        allowNull: true,
        comment: '备注'
      },
      createdBy: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '创建人ID',
        field: 'created_by',
        references: {
          model: 'users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      updatedBy: {
        type: DataTypes.INTEGER,
        allowNull: true,
        comment: '更新人ID',
        field: 'updated_by',
        references: {
          model: 'users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        field: 'created_at'
      },
      updatedAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        field: 'updated_at'
      }
    }, {
      comment: '监管任务表',
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci'
    });

    // 添加索引
    await queryInterface.addIndex('supervision_tasks', ['application_id']);
    await queryInterface.addIndex('supervision_tasks', ['policy_id']);
    await queryInterface.addIndex('supervision_tasks', ['customer_name']);
    await queryInterface.addIndex('supervision_tasks', ['status']);
    await queryInterface.addIndex('supervision_tasks', ['task_type']);
    await queryInterface.addIndex('supervision_tasks', ['assigned_to']);
    await queryInterface.addIndex('supervision_tasks', ['created_by']);
    await queryInterface.addIndex('supervision_tasks', ['created_at']);
    
    // 添加唯一索引
    await queryInterface.addIndex('supervision_tasks', ['application_id'], {
      unique: true,
      name: 'unique_application_id'
    });
    await queryInterface.addIndex('supervision_tasks', ['policy_id'], {
      unique: true,
      name: 'unique_policy_id'
    });
  },

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