import { MigrateUpArgs, MigrateDownArgs, sql } from '@payloadcms/db-postgres'

export async function up({ db }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   ALTER TYPE "public"."enum_church_recurring_schedule_week_of_month" ADD VALUE 'fifth' BEFORE 'last';
  ALTER TYPE "public"."enum_event_recurrence_rules_week_of_month" ADD VALUE 'fifth' BEFORE 'last';
  ALTER TYPE "public"."enum__event_v_version_recurrence_rules_week_of_month" ADD VALUE 'fifth' BEFORE 'last';`)
}

export async function down({ db }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   UPDATE "church_recurring_schedule" SET "week_of_month" = 'last' WHERE "week_of_month" = 'fifth';
  UPDATE "event_recurrence_rules" SET "week_of_month" = 'last' WHERE "week_of_month" = 'fifth';
  UPDATE "_event_v_version_recurrence_rules" SET "week_of_month" = 'last' WHERE "week_of_month" = 'fifth';
  ALTER TABLE "church_recurring_schedule" ALTER COLUMN "week_of_month" SET DATA TYPE text;
  DROP TYPE "public"."enum_church_recurring_schedule_week_of_month";
  CREATE TYPE "public"."enum_church_recurring_schedule_week_of_month" AS ENUM('first', 'second', 'third', 'fourth', 'last');
  ALTER TABLE "church_recurring_schedule" ALTER COLUMN "week_of_month" SET DATA TYPE "public"."enum_church_recurring_schedule_week_of_month" USING "week_of_month"::"public"."enum_church_recurring_schedule_week_of_month";
  ALTER TABLE "event_recurrence_rules" ALTER COLUMN "week_of_month" SET DATA TYPE text;
  DROP TYPE "public"."enum_event_recurrence_rules_week_of_month";
  CREATE TYPE "public"."enum_event_recurrence_rules_week_of_month" AS ENUM('first', 'second', 'third', 'fourth', 'last');
  ALTER TABLE "event_recurrence_rules" ALTER COLUMN "week_of_month" SET DATA TYPE "public"."enum_event_recurrence_rules_week_of_month" USING "week_of_month"::"public"."enum_event_recurrence_rules_week_of_month";
  ALTER TABLE "_event_v_version_recurrence_rules" ALTER COLUMN "week_of_month" SET DATA TYPE text;
  DROP TYPE "public"."enum__event_v_version_recurrence_rules_week_of_month";
  CREATE TYPE "public"."enum__event_v_version_recurrence_rules_week_of_month" AS ENUM('first', 'second', 'third', 'fourth', 'last');
  ALTER TABLE "_event_v_version_recurrence_rules" ALTER COLUMN "week_of_month" SET DATA TYPE "public"."enum__event_v_version_recurrence_rules_week_of_month" USING "week_of_month"::"public"."enum__event_v_version_recurrence_rules_week_of_month";`)
}
