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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TABLE "group_blocks_image_cards_items" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar,
  	"image_id" uuid,
  	"custom_link" varchar
  );
  
  CREATE TABLE "group_blocks_image_cards" (
  	"_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_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" uuid NOT NULL,
  	"path" varchar NOT NULL,
  	"pages_id" uuid,
  	"group_id" uuid
  );
  
  CREATE TABLE "_group_v_blocks_image_cards_items" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"title" varchar,
  	"image_id" uuid,
  	"custom_link" varchar,
  	"_uuid" varchar
  );
  
  CREATE TABLE "_group_v_blocks_image_cards" (
  	"_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_v_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" uuid NOT NULL,
  	"path" varchar NOT NULL,
  	"pages_id" uuid,
  	"group_id" uuid
  );
  
  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';
  ALTER TABLE "group_blocks_image_cards_items" ADD CONSTRAINT "group_blocks_image_cards_items_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "group_blocks_image_cards_items" ADD CONSTRAINT "group_blocks_image_cards_items_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group_blocks_image_cards"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "group_blocks_image_cards" ADD CONSTRAINT "group_blocks_image_cards_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "group_rels" ADD CONSTRAINT "group_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "group_rels" ADD CONSTRAINT "group_rels_pages_fk" FOREIGN KEY ("pages_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "group_rels" ADD CONSTRAINT "group_rels_group_fk" FOREIGN KEY ("group_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_group_v_blocks_image_cards_items" ADD CONSTRAINT "_group_v_blocks_image_cards_items_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "_group_v_blocks_image_cards_items" ADD CONSTRAINT "_group_v_blocks_image_cards_items_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_group_v_blocks_image_cards"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_group_v_blocks_image_cards" ADD CONSTRAINT "_group_v_blocks_image_cards_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_group_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_group_v_rels" ADD CONSTRAINT "_group_v_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."_group_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_group_v_rels" ADD CONSTRAINT "_group_v_rels_pages_fk" FOREIGN KEY ("pages_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_group_v_rels" ADD CONSTRAINT "_group_v_rels_group_fk" FOREIGN KEY ("group_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "group_blocks_image_cards_items_order_idx" ON "group_blocks_image_cards_items" USING btree ("_order");
  CREATE INDEX "group_blocks_image_cards_items_parent_id_idx" ON "group_blocks_image_cards_items" USING btree ("_parent_id");
  CREATE INDEX "group_blocks_image_cards_items_image_idx" ON "group_blocks_image_cards_items" USING btree ("image_id");
  CREATE INDEX "group_blocks_image_cards_order_idx" ON "group_blocks_image_cards" USING btree ("_order");
  CREATE INDEX "group_blocks_image_cards_parent_id_idx" ON "group_blocks_image_cards" USING btree ("_parent_id");
  CREATE INDEX "group_blocks_image_cards_path_idx" ON "group_blocks_image_cards" USING btree ("_path");
  CREATE INDEX "group_rels_order_idx" ON "group_rels" USING btree ("order");
  CREATE INDEX "group_rels_parent_idx" ON "group_rels" USING btree ("parent_id");
  CREATE INDEX "group_rels_path_idx" ON "group_rels" USING btree ("path");
  CREATE INDEX "group_rels_pages_id_idx" ON "group_rels" USING btree ("pages_id");
  CREATE INDEX "group_rels_group_id_idx" ON "group_rels" USING btree ("group_id");
  CREATE INDEX "_group_v_blocks_image_cards_items_order_idx" ON "_group_v_blocks_image_cards_items" USING btree ("_order");
  CREATE INDEX "_group_v_blocks_image_cards_items_parent_id_idx" ON "_group_v_blocks_image_cards_items" USING btree ("_parent_id");
  CREATE INDEX "_group_v_blocks_image_cards_items_image_idx" ON "_group_v_blocks_image_cards_items" USING btree ("image_id");
  CREATE INDEX "_group_v_blocks_image_cards_order_idx" ON "_group_v_blocks_image_cards" USING btree ("_order");
  CREATE INDEX "_group_v_blocks_image_cards_parent_id_idx" ON "_group_v_blocks_image_cards" USING btree ("_parent_id");
  CREATE INDEX "_group_v_blocks_image_cards_path_idx" ON "_group_v_blocks_image_cards" USING btree ("_path");
  CREATE INDEX "_group_v_rels_order_idx" ON "_group_v_rels" USING btree ("order");
  CREATE INDEX "_group_v_rels_parent_idx" ON "_group_v_rels" USING btree ("parent_id");
  CREATE INDEX "_group_v_rels_path_idx" ON "_group_v_rels" USING btree ("path");
  CREATE INDEX "_group_v_rels_pages_id_idx" ON "_group_v_rels" USING btree ("pages_id");
  CREATE INDEX "_group_v_rels_group_id_idx" ON "_group_v_rels" USING btree ("group_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "group_blocks_image_cards_items" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "group_blocks_image_cards" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "group_rels" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_group_v_blocks_image_cards_items" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_group_v_blocks_image_cards" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_group_v_rels" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "group_blocks_image_cards_items" CASCADE;
  DROP TABLE "group_blocks_image_cards" CASCADE;
  DROP TABLE "group_rels" CASCADE;
  DROP TABLE "_group_v_blocks_image_cards_items" CASCADE;
  DROP TABLE "_group_v_blocks_image_cards" CASCADE;
  DROP TABLE "_group_v_rels" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-19T12:20:19.986Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-19T12:20:20.277Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-13T12:20:20.339Z';`)
}
