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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TABLE "pages_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 "_pages_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-04-19T07:28:46.077Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-19T07:28:46.368Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-17T07:28:46.426Z';
  ALTER TABLE "pages_blocks_next_prev_buttons" ADD CONSTRAINT "pages_blocks_next_prev_buttons_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_pages_v_blocks_next_prev_buttons" ADD CONSTRAINT "_pages_v_blocks_next_prev_buttons_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_pages_v"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "pages_blocks_next_prev_buttons_order_idx" ON "pages_blocks_next_prev_buttons" USING btree ("_order");
  CREATE INDEX "pages_blocks_next_prev_buttons_parent_id_idx" ON "pages_blocks_next_prev_buttons" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_next_prev_buttons_path_idx" ON "pages_blocks_next_prev_buttons" USING btree ("_path");
  CREATE INDEX "_pages_v_blocks_next_prev_buttons_order_idx" ON "_pages_v_blocks_next_prev_buttons" USING btree ("_order");
  CREATE INDEX "_pages_v_blocks_next_prev_buttons_parent_id_idx" ON "_pages_v_blocks_next_prev_buttons" USING btree ("_parent_id");
  CREATE INDEX "_pages_v_blocks_next_prev_buttons_path_idx" ON "_pages_v_blocks_next_prev_buttons" USING btree ("_path");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "pages_blocks_next_prev_buttons" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_pages_v_blocks_next_prev_buttons" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "pages_blocks_next_prev_buttons" CASCADE;
  DROP TABLE "_pages_v_blocks_next_prev_buttons" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-19T12:14:50.471Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-19T12:14:50.803Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-16T12:14:50.859Z';`)
}
