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_type" AS ENUM('none', 'weekly', 'biweekly', 'monthlyByWeekday');
  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_recurrence_type" AS ENUM('none', 'weekly', 'biweekly', 'monthlyByWeekday');
  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_payload_jobs_log_task_slug" ADD VALUE 'generateEventOccurrences';
  ALTER TYPE "public"."enum_payload_jobs_task_slug" ADD VALUE 'generateEventOccurrences';
  CREATE TABLE "event_occurrence" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"event_id" uuid NOT NULL,
  	"date" timestamp(3) with time zone NOT NULL,
  	"cancelled" boolean DEFAULT false NOT NULL,
  	"generated" boolean DEFAULT false,
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
  );
  
  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 "recurrence_type" "enum_event_recurrence_type" DEFAULT 'none';
  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_recurrence_type" "enum__event_v_version_recurrence_type" DEFAULT 'none';
  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_v" ADD COLUMN "version_recurrence_end_date" timestamp(3) with time zone;
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "event_occurrence_id" uuid;
  ALTER TABLE "event_occurrence" ADD CONSTRAINT "event_occurrence_event_id_event_id_fk" FOREIGN KEY ("event_id") REFERENCES "public"."event"("id") ON DELETE set null ON UPDATE no action;
  CREATE INDEX "event_occurrence_event_idx" ON "event_occurrence" USING btree ("event_id");
  CREATE INDEX "event_occurrence_date_idx" ON "event_occurrence" USING btree ("date");
  CREATE INDEX "event_occurrence_updated_at_idx" ON "event_occurrence" USING btree ("updated_at");
  CREATE INDEX "event_occurrence_created_at_idx" ON "event_occurrence" USING btree ("created_at");
  ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_event_occurrence_fk" FOREIGN KEY ("event_occurrence_id") REFERENCES "public"."event_occurrence"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "payload_locked_documents_rels_event_occurrence_id_idx" ON "payload_locked_documents_rels" USING btree ("event_occurrence_id");
  ALTER TABLE "event" DROP COLUMN "is_recurring";
  ALTER TABLE "_event_v" DROP COLUMN "version_is_recurring";`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "event_occurrence" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "event_occurrence" CASCADE;
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_event_occurrence_fk";
  
  ALTER TABLE "payload_jobs_log" ALTER COLUMN "task_slug" SET DATA TYPE text;
  DROP TYPE "public"."enum_payload_jobs_log_task_slug";
  CREATE TYPE "public"."enum_payload_jobs_log_task_slug" AS ENUM('inline', 'generateRecurringMasses');
  ALTER TABLE "payload_jobs_log" ALTER COLUMN "task_slug" SET DATA TYPE "public"."enum_payload_jobs_log_task_slug" USING "task_slug"::"public"."enum_payload_jobs_log_task_slug";
  ALTER TABLE "payload_jobs" ALTER COLUMN "task_slug" SET DATA TYPE text;
  DROP TYPE "public"."enum_payload_jobs_task_slug";
  CREATE TYPE "public"."enum_payload_jobs_task_slug" AS ENUM('inline', 'generateRecurringMasses');
  ALTER TABLE "payload_jobs" ALTER COLUMN "task_slug" SET DATA TYPE "public"."enum_payload_jobs_task_slug" USING "task_slug"::"public"."enum_payload_jobs_task_slug";
  DROP INDEX "payload_locked_documents_rels_event_occurrence_id_idx";
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-19T07:51:55.082Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-19T07:51:55.418Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-17T07:51:55.487Z';
  ALTER TABLE "event" ADD COLUMN "is_recurring" boolean DEFAULT false;
  ALTER TABLE "_event_v" ADD COLUMN "version_is_recurring" boolean DEFAULT false;
  ALTER TABLE "event" DROP COLUMN "recurrence_type";
  ALTER TABLE "event" DROP COLUMN "day";
  ALTER TABLE "event" DROP COLUMN "week_of_month";
  ALTER TABLE "event" DROP COLUMN "biweekly_anchor";
  ALTER TABLE "event" DROP COLUMN "recurrence_end_date";
  ALTER TABLE "_event_v" DROP COLUMN "version_recurrence_type";
  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";
  ALTER TABLE "_event_v" DROP COLUMN "version_recurrence_end_date";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "event_occurrence_id";
  DROP TYPE "public"."enum_event_recurrence_type";
  DROP TYPE "public"."enum_event_day";
  DROP TYPE "public"."enum_event_week_of_month";
  DROP TYPE "public"."enum__event_v_version_recurrence_type";
  DROP TYPE "public"."enum__event_v_version_day";
  DROP TYPE "public"."enum__event_v_version_week_of_month";`)
}
