'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    // Add new enum values to the notification_rules event_type enum
    // PostgreSQL allows adding values to existing enums with ALTER TYPE

    // Add 'build_started' if it doesn't exist
    await queryInterface.sequelize.query(`
      DO $$
      BEGIN
        IF NOT EXISTS (
          SELECT 1 FROM pg_enum
          WHERE enumlabel = 'build_started'
          AND enumtypid = (SELECT oid FROM pg_type WHERE typname = 'enum_notification_rules_event_type')
        ) THEN
          ALTER TYPE "enum_notification_rules_event_type" ADD VALUE 'build_started';
        END IF;
      END
      $$;
    `);

    // Add 'session_timeout' if it doesn't exist
    await queryInterface.sequelize.query(`
      DO $$
      BEGIN
        IF NOT EXISTS (
          SELECT 1 FROM pg_enum
          WHERE enumlabel = 'session_timeout'
          AND enumtypid = (SELECT oid FROM pg_type WHERE typname = 'enum_notification_rules_event_type')
        ) THEN
          ALTER TYPE "enum_notification_rules_event_type" ADD VALUE 'session_timeout';
        END IF;
      END
      $$;
    `);

    console.log('Added build_started and session_timeout to notification event types');
  },

  async down(queryInterface, Sequelize) {
    // PostgreSQL doesn't support removing values from enums directly
    // The safest approach is to leave the enum values in place
    // They won't cause any issues if unused
    console.log(
      'Note: Enum values cannot be removed in PostgreSQL. The values will remain but be unused.'
    );
  },
};
