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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "event" RENAME COLUMN "recurrence_end_date" TO "end_date";
  ALTER TABLE "_event_v" RENAME COLUMN "version_recurrence_end_date" TO "version_end_date";
  ALTER TABLE "event" ALTER COLUMN "recurrence_type" SET DATA TYPE text;
  ALTER TABLE "event" ALTER COLUMN "recurrence_type" SET DEFAULT 'none'::text;
  DROP TYPE "public"."enum_event_recurrence_type";
  CREATE TYPE "public"."enum_event_recurrence_type" AS ENUM('none', 'weekly', 'biweekly');
  ALTER TABLE "event" ALTER COLUMN "recurrence_type" SET DEFAULT 'none'::"public"."enum_event_recurrence_type";
  ALTER TABLE "event" ALTER COLUMN "recurrence_type" SET DATA TYPE "public"."enum_event_recurrence_type" USING "recurrence_type"::"public"."enum_event_recurrence_type";
  ALTER TABLE "_event_v" ALTER COLUMN "version_recurrence_type" SET DATA TYPE text;
  ALTER TABLE "_event_v" ALTER COLUMN "version_recurrence_type" SET DEFAULT 'none'::text;
  DROP TYPE "public"."enum__event_v_version_recurrence_type";
  CREATE TYPE "public"."enum__event_v_version_recurrence_type" AS ENUM('none', 'weekly', 'biweekly');
  ALTER TABLE "_event_v" ALTER COLUMN "version_recurrence_type" SET DEFAULT 'none'::"public"."enum__event_v_version_recurrence_type";
  ALTER TABLE "_event_v" ALTER COLUMN "version_recurrence_type" SET DATA TYPE "public"."enum__event_v_version_recurrence_type" USING "version_recurrence_type"::"public"."enum__event_v_version_recurrence_type";
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-19T11:47:26.630Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-19T11:47:26.914Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-17T11:47:26.972Z';
  ALTER TABLE "event" DROP COLUMN "day";
  ALTER TABLE "event" DROP COLUMN "week_of_month";
  ALTER TABLE "event" DROP COLUMN "biweekly_anchor";
  ALTER TABLE "_event_v" DROP COLUMN "version_day";
  ALTER TABLE "_event_v" DROP COLUMN "version_week_of_month";
  ALTER TABLE "_event_v" DROP COLUMN "version_biweekly_anchor";
  DROP TYPE "public"."enum_event_day";
  DROP TYPE "public"."enum_event_week_of_month";
  DROP TYPE "public"."enum__event_v_version_day";
  DROP TYPE "public"."enum__event_v_version_week_of_month";`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   CREATE TYPE "public"."enum_event_day" AS ENUM('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
  CREATE TYPE "public"."enum_event_week_of_month" AS ENUM('first', 'second', 'third', 'fourth', 'last');
  CREATE TYPE "public"."enum__event_v_version_day" AS ENUM('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
  CREATE TYPE "public"."enum__event_v_version_week_of_month" AS ENUM('first', 'second', 'third', 'fourth', 'last');
  ALTER TYPE "public"."enum_event_recurrence_type" ADD VALUE 'monthlyByWeekday';
  ALTER TYPE "public"."enum__event_v_version_recurrence_type" ADD VALUE 'monthlyByWeekday';
  ALTER TABLE "_event_v" RENAME COLUMN "version_end_date" TO "version_recurrence_end_date";
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-19T11:18:54.302Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-19T11:18:54.627Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-17T11:18:54.690Z';
  ALTER TABLE "event" ADD COLUMN "day" "enum_event_day";
  ALTER TABLE "event" ADD COLUMN "week_of_month" "enum_event_week_of_month";
  ALTER TABLE "event" ADD COLUMN "biweekly_anchor" timestamp(3) with time zone;
  ALTER TABLE "event" ADD COLUMN "recurrence_end_date" timestamp(3) with time zone;
  ALTER TABLE "_event_v" ADD COLUMN "version_day" "enum__event_v_version_day";
  ALTER TABLE "_event_v" ADD COLUMN "version_week_of_month" "enum__event_v_version_week_of_month";
  ALTER TABLE "_event_v" ADD COLUMN "version_biweekly_anchor" timestamp(3) with time zone;
  ALTER TABLE "event" DROP COLUMN "end_date";`)
}
