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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TYPE "public"."enum_event_recurrence_rules_frequency" AS ENUM('daily', 'weekly', 'monthlyByDate', 'monthlyByWeekday');
  CREATE TYPE "public"."enum_event_recurrence_rules_weekday" AS ENUM('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
  CREATE TYPE "public"."enum_event_recurrence_rules_week_of_month" AS ENUM('first', 'second', 'third', 'fourth', 'last');
  CREATE TYPE "public"."enum__event_v_version_recurrence_rules_frequency" AS ENUM('daily', 'weekly', 'monthlyByDate', 'monthlyByWeekday');
  CREATE TYPE "public"."enum__event_v_version_recurrence_rules_weekday" AS ENUM('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
  CREATE TYPE "public"."enum__event_v_version_recurrence_rules_week_of_month" AS ENUM('first', 'second', 'third', 'fourth', 'last');
  ALTER TYPE "public"."enum_event_recurrence_type" ADD VALUE 'daily' BEFORE 'weekly';
  ALTER TYPE "public"."enum_event_recurrence_type" ADD VALUE 'monthlyByDate';
  ALTER TYPE "public"."enum_event_recurrence_type" ADD VALUE 'monthlyByWeekday';
  ALTER TYPE "public"."enum_event_recurrence_type" ADD VALUE 'custom';
  ALTER TYPE "public"."enum__event_v_version_recurrence_type" ADD VALUE 'daily' BEFORE 'weekly';
  ALTER TYPE "public"."enum__event_v_version_recurrence_type" ADD VALUE 'monthlyByDate';
  ALTER TYPE "public"."enum__event_v_version_recurrence_type" ADD VALUE 'monthlyByWeekday';
  ALTER TYPE "public"."enum__event_v_version_recurrence_type" ADD VALUE 'custom';
  CREATE TABLE "event_recurrence_rules" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"frequency" "enum_event_recurrence_rules_frequency",
  	"weekday" "enum_event_recurrence_rules_weekday",
  	"week_of_month" "enum_event_recurrence_rules_week_of_month",
  	"day_of_month" numeric
  );
  
  CREATE TABLE "_event_v_version_recurrence_rules" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"frequency" "enum__event_v_version_recurrence_rules_frequency",
  	"weekday" "enum__event_v_version_recurrence_rules_weekday",
  	"week_of_month" "enum__event_v_version_recurrence_rules_week_of_month",
  	"day_of_month" numeric,
  	"_uuid" varchar
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-26T11:53:10.432Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-26T11:53:10.804Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-23T11:53:10.907Z';
  ALTER TABLE "event_recurrence_rules" ADD CONSTRAINT "event_recurrence_rules_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."event"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_event_v_version_recurrence_rules" ADD CONSTRAINT "_event_v_version_recurrence_rules_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_event_v"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "event_recurrence_rules_order_idx" ON "event_recurrence_rules" USING btree ("_order");
  CREATE INDEX "event_recurrence_rules_parent_id_idx" ON "event_recurrence_rules" USING btree ("_parent_id");
  CREATE INDEX "_event_v_version_recurrence_rules_order_idx" ON "_event_v_version_recurrence_rules" USING btree ("_order");
  CREATE INDEX "_event_v_version_recurrence_rules_parent_id_idx" ON "_event_v_version_recurrence_rules" USING btree ("_parent_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "event_recurrence_rules" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_event_v_version_recurrence_rules" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "event_recurrence_rules" CASCADE;
  DROP TABLE "_event_v_version_recurrence_rules" CASCADE;
  ALTER TABLE "event" ALTER COLUMN "recurrence_type" SET DATA TYPE text;
  ALTER TABLE "event" ALTER COLUMN "recurrence_type" SET DEFAULT 'none'::text;
  UPDATE "event" SET "recurrence_type" = 'none' WHERE "recurrence_type" NOT IN ('none', 'weekly', 'biweekly');
  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;
  UPDATE "_event_v" SET "version_recurrence_type" = 'none' WHERE "version_recurrence_type" NOT IN ('none', 'weekly', 'biweekly');
  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';
  DROP TYPE "public"."enum_event_recurrence_rules_frequency";
  DROP TYPE "public"."enum_event_recurrence_rules_weekday";
  DROP TYPE "public"."enum_event_recurrence_rules_week_of_month";
  DROP TYPE "public"."enum__event_v_version_recurrence_rules_frequency";
  DROP TYPE "public"."enum__event_v_version_recurrence_rules_weekday";
  DROP TYPE "public"."enum__event_v_version_recurrence_rules_week_of_month";`)
}
