'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 1. Add 'skipped' to the ENUM status
    await queryInterface.changeColumn('MerchantWebhookLogs', 'status', {
      type: Sequelize.ENUM('pending', 'delivered', 'failed', 'skipped'),
      defaultValue: 'pending',
      allowNull: false
    });

    // 2. Add 'webhookUrl' column
    await queryInterface.addColumn('MerchantWebhookLogs', 'webhookUrl', {
      type: Sequelize.STRING,
      allowNull: true,
      after: 'event'
    });

    // 3. Add 'customerId' column (the "userid" the webhook is about)
    await queryInterface.addColumn('MerchantWebhookLogs', 'customerId', {
      type: Sequelize.UUID,
      allowNull: true,
      after: 'merchantId',
      references: { model: 'Customers', key: 'id' },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL'
    });
  },

  down: async (queryInterface, Sequelize) => {
    // 1. Remove columns
    await queryInterface.removeColumn('MerchantWebhookLogs', 'customerId');
    await queryInterface.removeColumn('MerchantWebhookLogs', 'webhookUrl');

    // 2. Revert back to original ENUM
    await queryInterface.changeColumn('MerchantWebhookLogs', 'status', {
      type: Sequelize.ENUM('pending', 'delivered', 'failed'),
      defaultValue: 'pending',
      allowNull: false
    });
  }
};
