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

export async function up({ payload, req }: MigrateUpArgs): Promise<void> {
  await payload.db.drizzle.execute(sql`
   CREATE TABLE IF NOT EXISTS "calendar" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"date" timestamp(3) with time zone DEFAULT '2025-02-02T14:51:45.077Z' NOT NULL,
  	"document_id" uuid NOT NULL,
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
  );
  
  CREATE TABLE IF NOT EXISTS "calendar_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" uuid NOT NULL,
  	"path" varchar NOT NULL,
  	"parish_id" uuid
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2025-02-02T14:51:44.981Z';
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "calendar_id" uuid;
  DO $$ BEGIN
   ALTER TABLE "calendar" ADD CONSTRAINT "calendar_document_id_documents_id_fk" FOREIGN KEY ("document_id") REFERENCES "public"."documents"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "calendar_rels" ADD CONSTRAINT "calendar_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."calendar"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "calendar_rels" ADD CONSTRAINT "calendar_rels_parish_fk" FOREIGN KEY ("parish_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  CREATE INDEX IF NOT EXISTS "calendar_document_idx" ON "calendar" USING btree ("document_id");
  CREATE INDEX IF NOT EXISTS "calendar_updated_at_idx" ON "calendar" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "calendar_created_at_idx" ON "calendar" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "calendar_rels_order_idx" ON "calendar_rels" USING btree ("order");
  CREATE INDEX IF NOT EXISTS "calendar_rels_parent_idx" ON "calendar_rels" USING btree ("parent_id");
  CREATE INDEX IF NOT EXISTS "calendar_rels_path_idx" ON "calendar_rels" USING btree ("path");
  CREATE INDEX IF NOT EXISTS "calendar_rels_parish_id_idx" ON "calendar_rels" USING btree ("parish_id");
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_calendar_fk" FOREIGN KEY ("calendar_id") REFERENCES "public"."calendar"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_calendar_id_idx" ON "payload_locked_documents_rels" USING btree ("calendar_id");`)
}

export async function down({ payload, req }: MigrateDownArgs): Promise<void> {
  await payload.db.drizzle.execute(sql`
   ALTER TABLE "calendar" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "calendar_rels" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "calendar" CASCADE;
  DROP TABLE "calendar_rels" CASCADE;
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_calendar_fk";
  
  DROP INDEX IF EXISTS "payload_locked_documents_rels_calendar_id_idx";
  ALTER TABLE "announcement" ALTER COLUMN "date" DROP DEFAULT;
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN IF EXISTS "calendar_id";`)
}
