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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "pages_blocks_collapsible_image_with_text" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "pages_blocks_publication_and_newsletter" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "pages_blocks_collapsible_image_with_text" CASCADE;
  DROP TABLE "pages_blocks_publication_and_newsletter" CASCADE;
  DROP INDEX "_blog_v_autosave_idx";
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-03-15T10:59:47.266Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-03-15T10:59:47.547Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-04-10T09:59:47.604Z';
  ALTER TABLE "_blog_v" DROP COLUMN "autosave";
  DROP TYPE "public"."enum_pages_blocks_collapsible_image_with_text_background_color";
  DROP TYPE "public"."enum_pages_blocks_collapsible_image_with_text_schema";`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   CREATE TYPE "public"."enum_pages_blocks_collapsible_image_with_text_background_color" AS ENUM('none', 'soft', 'off-white');
  CREATE TYPE "public"."enum_pages_blocks_collapsible_image_with_text_schema" AS ENUM('base', 'contrast');
  CREATE TABLE "pages_blocks_collapsible_image_with_text" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar NOT NULL,
  	"text" varchar NOT NULL,
  	"image_id" uuid NOT NULL,
  	"content" jsonb NOT NULL,
  	"background_color" "enum_pages_blocks_collapsible_image_with_text_background_color" DEFAULT 'none',
  	"schema" "enum_pages_blocks_collapsible_image_with_text_schema" DEFAULT 'base',
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_publication_and_newsletter" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"block_name" varchar
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-03-15T14:38:00.254Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-03-15T14:38:00.534Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-04-09T13:38:00.592Z';
  ALTER TABLE "_blog_v" ADD COLUMN "autosave" boolean;
  ALTER TABLE "pages_blocks_collapsible_image_with_text" ADD CONSTRAINT "pages_blocks_collapsible_image_with_text_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "pages_blocks_collapsible_image_with_text" ADD CONSTRAINT "pages_blocks_collapsible_image_with_text_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_publication_and_newsletter" ADD CONSTRAINT "pages_blocks_publication_and_newsletter_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "pages_blocks_collapsible_image_with_text_order_idx" ON "pages_blocks_collapsible_image_with_text" USING btree ("_order");
  CREATE INDEX "pages_blocks_collapsible_image_with_text_parent_id_idx" ON "pages_blocks_collapsible_image_with_text" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_collapsible_image_with_text_path_idx" ON "pages_blocks_collapsible_image_with_text" USING btree ("_path");
  CREATE INDEX "pages_blocks_collapsible_image_with_text_image_idx" ON "pages_blocks_collapsible_image_with_text" USING btree ("image_id");
  CREATE INDEX "pages_blocks_publication_and_newsletter_order_idx" ON "pages_blocks_publication_and_newsletter" USING btree ("_order");
  CREATE INDEX "pages_blocks_publication_and_newsletter_parent_id_idx" ON "pages_blocks_publication_and_newsletter" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_publication_and_newsletter_path_idx" ON "pages_blocks_publication_and_newsletter" USING btree ("_path");
  CREATE INDEX "_blog_v_autosave_idx" ON "_blog_v" USING btree ("autosave");`)
}
