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_next_prev_buttons" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"prev_text" varchar,
  	"prev_href" varchar,
  	"next_text" varchar,
  	"next_href" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "_parish_v_blocks_next_prev_buttons" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"prev_text" varchar,
  	"prev_href" varchar,
  	"next_text" varchar,
  	"next_href" varchar,
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "group_blocks_next_prev_buttons" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"prev_text" varchar,
  	"prev_href" varchar,
  	"next_text" varchar,
  	"next_href" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "_group_v_blocks_next_prev_buttons" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"prev_text" varchar,
  	"prev_href" varchar,
  	"next_text" varchar,
  	"next_href" varchar,
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-05-10T07:03:55.605Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-05-10T07:03:55.910Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-06-03T07:03:55.993Z';
  ALTER TABLE "parish_blocks_next_prev_buttons" ADD CONSTRAINT "parish_blocks_next_prev_buttons_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_parish_v_blocks_next_prev_buttons" ADD CONSTRAINT "_parish_v_blocks_next_prev_buttons_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_parish_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "group_blocks_next_prev_buttons" ADD CONSTRAINT "group_blocks_next_prev_buttons_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_group_v_blocks_next_prev_buttons" ADD CONSTRAINT "_group_v_blocks_next_prev_buttons_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_group_v"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "parish_blocks_next_prev_buttons_order_idx" ON "parish_blocks_next_prev_buttons" USING btree ("_order");
  CREATE INDEX "parish_blocks_next_prev_buttons_parent_id_idx" ON "parish_blocks_next_prev_buttons" USING btree ("_parent_id");
  CREATE INDEX "parish_blocks_next_prev_buttons_path_idx" ON "parish_blocks_next_prev_buttons" USING btree ("_path");
  CREATE INDEX "_parish_v_blocks_next_prev_buttons_order_idx" ON "_parish_v_blocks_next_prev_buttons" USING btree ("_order");
  CREATE INDEX "_parish_v_blocks_next_prev_buttons_parent_id_idx" ON "_parish_v_blocks_next_prev_buttons" USING btree ("_parent_id");
  CREATE INDEX "_parish_v_blocks_next_prev_buttons_path_idx" ON "_parish_v_blocks_next_prev_buttons" USING btree ("_path");
  CREATE INDEX "group_blocks_next_prev_buttons_order_idx" ON "group_blocks_next_prev_buttons" USING btree ("_order");
  CREATE INDEX "group_blocks_next_prev_buttons_parent_id_idx" ON "group_blocks_next_prev_buttons" USING btree ("_parent_id");
  CREATE INDEX "group_blocks_next_prev_buttons_path_idx" ON "group_blocks_next_prev_buttons" USING btree ("_path");
  CREATE INDEX "_group_v_blocks_next_prev_buttons_order_idx" ON "_group_v_blocks_next_prev_buttons" USING btree ("_order");
  CREATE INDEX "_group_v_blocks_next_prev_buttons_parent_id_idx" ON "_group_v_blocks_next_prev_buttons" USING btree ("_parent_id");
  CREATE INDEX "_group_v_blocks_next_prev_buttons_path_idx" ON "_group_v_blocks_next_prev_buttons" USING btree ("_path");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "parish_blocks_next_prev_buttons" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_parish_v_blocks_next_prev_buttons" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "group_blocks_next_prev_buttons" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_group_v_blocks_next_prev_buttons" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "parish_blocks_next_prev_buttons" CASCADE;
  DROP TABLE "_parish_v_blocks_next_prev_buttons" CASCADE;
  DROP TABLE "group_blocks_next_prev_buttons" CASCADE;
  DROP TABLE "_group_v_blocks_next_prev_buttons" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-05-03T12:11:04.492Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-05-03T12:11:04.791Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-29T12:11:04.854Z';`)
}
