import { QueryInterface, DataTypes } from 'sequelize';

export async function up(queryInterface: QueryInterface): Promise<void> {
  // Add pause fields to workflow_requests table
  await queryInterface.addColumn('workflow_requests', 'is_paused', {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false
  });

  await queryInterface.addColumn('workflow_requests', 'paused_at', {
    type: DataTypes.DATE,
    allowNull: true
  });

  await queryInterface.addColumn('workflow_requests', 'paused_by', {
    type: DataTypes.UUID,
    allowNull: true,
    references: {
      model: 'users',
      key: 'user_id'
    }
  });

  await queryInterface.addColumn('workflow_requests', 'pause_reason', {
    type: DataTypes.TEXT,
    allowNull: true
  });

  await queryInterface.addColumn('workflow_requests', 'pause_resume_date', {
    type: DataTypes.DATE,
    allowNull: true
  });

  await queryInterface.addColumn('workflow_requests', 'pause_tat_snapshot', {
    type: DataTypes.JSONB,
    allowNull: true
  });

  // Create index on is_paused for faster queries
  await queryInterface.sequelize.query(
    'CREATE INDEX IF NOT EXISTS "workflow_requests_is_paused" ON "workflow_requests" ("is_paused");'
  );

  // Create index on pause_resume_date for auto-resume job
  await queryInterface.sequelize.query(
    'CREATE INDEX IF NOT EXISTS "workflow_requests_pause_resume_date" ON "workflow_requests" ("pause_resume_date") WHERE "is_paused" = true;'
  );
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.removeColumn('workflow_requests', 'pause_tat_snapshot');
  await queryInterface.removeColumn('workflow_requests', 'pause_resume_date');
  await queryInterface.removeColumn('workflow_requests', 'pause_reason');
  await queryInterface.removeColumn('workflow_requests', 'paused_by');
  await queryInterface.removeColumn('workflow_requests', 'paused_at');
  await queryInterface.removeColumn('workflow_requests', 'is_paused');
}

