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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TABLE "parish_blocks_collapsibles_items" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar,
  	"content" jsonb
  );
  
  CREATE TABLE "parish_blocks_collapsibles" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE "_parish_v_blocks_collapsibles_items" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"title" varchar,
  	"content" jsonb,
  	"_uuid" varchar
  );
  
  CREATE TABLE "_parish_v_blocks_collapsibles" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "group_blocks_collapsibles_items" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar,
  	"content" jsonb
  );
  
  CREATE TABLE "group_blocks_collapsibles" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE "_group_v_blocks_collapsibles_items" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"title" varchar,
  	"content" jsonb,
  	"_uuid" varchar
  );
  
  CREATE TABLE "_group_v_blocks_collapsibles" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_collapsibles_items" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar,
  	"content" jsonb
  );
  
  CREATE TABLE "pages_blocks_collapsibles" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE "_pages_v_blocks_collapsibles_items" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"title" varchar,
  	"content" jsonb,
  	"_uuid" varchar
  );
  
  CREATE TABLE "_pages_v_blocks_collapsibles" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-19T11:54:45.762Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-19T11:54:46.057Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-16T11:54:46.114Z';
  ALTER TABLE "parish_blocks_collapsibles_items" ADD CONSTRAINT "parish_blocks_collapsibles_items_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."parish_blocks_collapsibles"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "parish_blocks_collapsibles" ADD CONSTRAINT "parish_blocks_collapsibles_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_parish_v_blocks_collapsibles_items" ADD CONSTRAINT "_parish_v_blocks_collapsibles_items_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_parish_v_blocks_collapsibles"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_parish_v_blocks_collapsibles" ADD CONSTRAINT "_parish_v_blocks_collapsibles_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_parish_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "group_blocks_collapsibles_items" ADD CONSTRAINT "group_blocks_collapsibles_items_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group_blocks_collapsibles"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "group_blocks_collapsibles" ADD CONSTRAINT "group_blocks_collapsibles_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_group_v_blocks_collapsibles_items" ADD CONSTRAINT "_group_v_blocks_collapsibles_items_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_group_v_blocks_collapsibles"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_group_v_blocks_collapsibles" ADD CONSTRAINT "_group_v_blocks_collapsibles_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_group_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_collapsibles_items" ADD CONSTRAINT "pages_blocks_collapsibles_items_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_collapsibles"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_collapsibles" ADD CONSTRAINT "pages_blocks_collapsibles_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_pages_v_blocks_collapsibles_items" ADD CONSTRAINT "_pages_v_blocks_collapsibles_items_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_pages_v_blocks_collapsibles"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_pages_v_blocks_collapsibles" ADD CONSTRAINT "_pages_v_blocks_collapsibles_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_pages_v"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "parish_blocks_collapsibles_items_order_idx" ON "parish_blocks_collapsibles_items" USING btree ("_order");
  CREATE INDEX "parish_blocks_collapsibles_items_parent_id_idx" ON "parish_blocks_collapsibles_items" USING btree ("_parent_id");
  CREATE INDEX "parish_blocks_collapsibles_order_idx" ON "parish_blocks_collapsibles" USING btree ("_order");
  CREATE INDEX "parish_blocks_collapsibles_parent_id_idx" ON "parish_blocks_collapsibles" USING btree ("_parent_id");
  CREATE INDEX "parish_blocks_collapsibles_path_idx" ON "parish_blocks_collapsibles" USING btree ("_path");
  CREATE INDEX "_parish_v_blocks_collapsibles_items_order_idx" ON "_parish_v_blocks_collapsibles_items" USING btree ("_order");
  CREATE INDEX "_parish_v_blocks_collapsibles_items_parent_id_idx" ON "_parish_v_blocks_collapsibles_items" USING btree ("_parent_id");
  CREATE INDEX "_parish_v_blocks_collapsibles_order_idx" ON "_parish_v_blocks_collapsibles" USING btree ("_order");
  CREATE INDEX "_parish_v_blocks_collapsibles_parent_id_idx" ON "_parish_v_blocks_collapsibles" USING btree ("_parent_id");
  CREATE INDEX "_parish_v_blocks_collapsibles_path_idx" ON "_parish_v_blocks_collapsibles" USING btree ("_path");
  CREATE INDEX "group_blocks_collapsibles_items_order_idx" ON "group_blocks_collapsibles_items" USING btree ("_order");
  CREATE INDEX "group_blocks_collapsibles_items_parent_id_idx" ON "group_blocks_collapsibles_items" USING btree ("_parent_id");
  CREATE INDEX "group_blocks_collapsibles_order_idx" ON "group_blocks_collapsibles" USING btree ("_order");
  CREATE INDEX "group_blocks_collapsibles_parent_id_idx" ON "group_blocks_collapsibles" USING btree ("_parent_id");
  CREATE INDEX "group_blocks_collapsibles_path_idx" ON "group_blocks_collapsibles" USING btree ("_path");
  CREATE INDEX "_group_v_blocks_collapsibles_items_order_idx" ON "_group_v_blocks_collapsibles_items" USING btree ("_order");
  CREATE INDEX "_group_v_blocks_collapsibles_items_parent_id_idx" ON "_group_v_blocks_collapsibles_items" USING btree ("_parent_id");
  CREATE INDEX "_group_v_blocks_collapsibles_order_idx" ON "_group_v_blocks_collapsibles" USING btree ("_order");
  CREATE INDEX "_group_v_blocks_collapsibles_parent_id_idx" ON "_group_v_blocks_collapsibles" USING btree ("_parent_id");
  CREATE INDEX "_group_v_blocks_collapsibles_path_idx" ON "_group_v_blocks_collapsibles" USING btree ("_path");
  CREATE INDEX "pages_blocks_collapsibles_items_order_idx" ON "pages_blocks_collapsibles_items" USING btree ("_order");
  CREATE INDEX "pages_blocks_collapsibles_items_parent_id_idx" ON "pages_blocks_collapsibles_items" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_collapsibles_order_idx" ON "pages_blocks_collapsibles" USING btree ("_order");
  CREATE INDEX "pages_blocks_collapsibles_parent_id_idx" ON "pages_blocks_collapsibles" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_collapsibles_path_idx" ON "pages_blocks_collapsibles" USING btree ("_path");
  CREATE INDEX "_pages_v_blocks_collapsibles_items_order_idx" ON "_pages_v_blocks_collapsibles_items" USING btree ("_order");
  CREATE INDEX "_pages_v_blocks_collapsibles_items_parent_id_idx" ON "_pages_v_blocks_collapsibles_items" USING btree ("_parent_id");
  CREATE INDEX "_pages_v_blocks_collapsibles_order_idx" ON "_pages_v_blocks_collapsibles" USING btree ("_order");
  CREATE INDEX "_pages_v_blocks_collapsibles_parent_id_idx" ON "_pages_v_blocks_collapsibles" USING btree ("_parent_id");
  CREATE INDEX "_pages_v_blocks_collapsibles_path_idx" ON "_pages_v_blocks_collapsibles" USING btree ("_path");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "parish_blocks_collapsibles_items" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "parish_blocks_collapsibles" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_parish_v_blocks_collapsibles_items" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_parish_v_blocks_collapsibles" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "group_blocks_collapsibles_items" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "group_blocks_collapsibles" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_group_v_blocks_collapsibles_items" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_group_v_blocks_collapsibles" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "pages_blocks_collapsibles_items" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "pages_blocks_collapsibles" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_pages_v_blocks_collapsibles_items" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_pages_v_blocks_collapsibles" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "parish_blocks_collapsibles_items" CASCADE;
  DROP TABLE "parish_blocks_collapsibles" CASCADE;
  DROP TABLE "_parish_v_blocks_collapsibles_items" CASCADE;
  DROP TABLE "_parish_v_blocks_collapsibles" CASCADE;
  DROP TABLE "group_blocks_collapsibles_items" CASCADE;
  DROP TABLE "group_blocks_collapsibles" CASCADE;
  DROP TABLE "_group_v_blocks_collapsibles_items" CASCADE;
  DROP TABLE "_group_v_blocks_collapsibles" CASCADE;
  DROP TABLE "pages_blocks_collapsibles_items" CASCADE;
  DROP TABLE "pages_blocks_collapsibles" CASCADE;
  DROP TABLE "_pages_v_blocks_collapsibles_items" CASCADE;
  DROP TABLE "_pages_v_blocks_collapsibles" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-19T09:09:53.986Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-19T09:09:54.280Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-16T09:09:54.338Z';`)
}
