"use strict";

/**
 * Replaces stripeDisputeStatus enum with all valid Stripe dispute statuses.
 * Previous enum had: open, won, lost, warning_closed
 * Stripe uses: needs_response, under_review, won, lost,
 *              warning_needs_response, warning_under_review, warning_closed
 */
module.exports = {
  up: async (queryInterface) => {
    // Create new enum type with correct Stripe statuses
    await queryInterface.sequelize.query(`
      CREATE TYPE "enum_Rentals_stripeDisputeStatus_new" AS ENUM (
        'needs_response',
        'under_review',
        'won',
        'lost',
        'warning_needs_response',
        'warning_under_review',
        'warning_closed'
      );
    `);

    // Alter column to use new type
    await queryInterface.sequelize.query(`
      ALTER TABLE "Rentals"
      ALTER COLUMN "stripeDisputeStatus"
      TYPE "enum_Rentals_stripeDisputeStatus_new"
      USING "stripeDisputeStatus"::text::"enum_Rentals_stripeDisputeStatus_new";
    `);

    // Drop old enum type
    await queryInterface.sequelize.query(`
      DROP TYPE "enum_Rentals_stripeDisputeStatus";
    `);

    // Rename new type to original name
    await queryInterface.sequelize.query(`
      ALTER TYPE "enum_Rentals_stripeDisputeStatus_new"
      RENAME TO "enum_Rentals_stripeDisputeStatus";
    `);
  },

  down: async (queryInterface) => {
    await queryInterface.sequelize.query(`
      CREATE TYPE "enum_Rentals_stripeDisputeStatus_old" AS ENUM (
        'open', 'won', 'lost', 'warning_closed'
      );
    `);

    await queryInterface.sequelize.query(`
      ALTER TABLE "Rentals"
      ALTER COLUMN "stripeDisputeStatus"
      TYPE "enum_Rentals_stripeDisputeStatus_old"
      USING "stripeDisputeStatus"::text::"enum_Rentals_stripeDisputeStatus_old";
    `);

    await queryInterface.sequelize.query(`
      DROP TYPE "enum_Rentals_stripeDisputeStatus";
    `);

    await queryInterface.sequelize.query(`
      ALTER TYPE "enum_Rentals_stripeDisputeStatus_old"
      RENAME TO "enum_Rentals_stripeDisputeStatus";
    `);
  },
};
