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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TYPE "public"."enum_parish_blocks_image_with_text_title_color" AS ENUM('base', 'contrast');
  CREATE TYPE "public"."enum_parish_blocks_image_with_text_background_color" AS ENUM('none', 'soft', 'off-white');
  CREATE TYPE "public"."enum_parish_blocks_image_with_text_image_position" AS ENUM('left', 'right');
  CREATE TYPE "public"."enum__parish_v_blocks_image_with_text_title_color" AS ENUM('base', 'contrast');
  CREATE TYPE "public"."enum__parish_v_blocks_image_with_text_background_color" AS ENUM('none', 'soft', 'off-white');
  CREATE TYPE "public"."enum__parish_v_blocks_image_with_text_image_position" AS ENUM('left', 'right');
  CREATE TYPE "public"."enum_group_blocks_image_with_text_title_color" AS ENUM('base', 'contrast');
  CREATE TYPE "public"."enum_group_blocks_image_with_text_background_color" AS ENUM('none', 'soft', 'off-white');
  CREATE TYPE "public"."enum_group_blocks_image_with_text_image_position" AS ENUM('left', 'right');
  CREATE TYPE "public"."enum__group_v_blocks_image_with_text_title_color" AS ENUM('base', 'contrast');
  CREATE TYPE "public"."enum__group_v_blocks_image_with_text_background_color" AS ENUM('none', 'soft', 'off-white');
  CREATE TYPE "public"."enum__group_v_blocks_image_with_text_image_position" AS ENUM('left', 'right');
  CREATE TYPE "public"."enum_pages_blocks_image_with_text_title_color" AS ENUM('base', 'contrast');
  CREATE TYPE "public"."enum_pages_blocks_image_with_text_background_color" AS ENUM('none', 'soft', 'off-white');
  CREATE TYPE "public"."enum_pages_blocks_image_with_text_image_position" AS ENUM('left', 'right');
  CREATE TYPE "public"."enum__pages_v_blocks_image_with_text_title_color" AS ENUM('base', 'contrast');
  CREATE TYPE "public"."enum__pages_v_blocks_image_with_text_background_color" AS ENUM('none', 'soft', 'off-white');
  CREATE TYPE "public"."enum__pages_v_blocks_image_with_text_image_position" AS ENUM('left', 'right');
  CREATE TABLE "parish_blocks_image_with_text" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar,
  	"title_color" "enum_parish_blocks_image_with_text_title_color" DEFAULT 'base',
  	"background_color" "enum_parish_blocks_image_with_text_background_color" DEFAULT 'none',
  	"image_id" uuid,
  	"image_position" "enum_parish_blocks_image_with_text_image_position" DEFAULT 'left',
  	"text" jsonb,
  	"block_name" varchar
  );
  
  CREATE TABLE "_parish_v_blocks_image_with_text" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"title" varchar,
  	"title_color" "enum__parish_v_blocks_image_with_text_title_color" DEFAULT 'base',
  	"background_color" "enum__parish_v_blocks_image_with_text_background_color" DEFAULT 'none',
  	"image_id" uuid,
  	"image_position" "enum__parish_v_blocks_image_with_text_image_position" DEFAULT 'left',
  	"text" jsonb,
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "group_blocks_image_with_text" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar,
  	"title_color" "enum_group_blocks_image_with_text_title_color" DEFAULT 'base',
  	"background_color" "enum_group_blocks_image_with_text_background_color" DEFAULT 'none',
  	"image_id" uuid,
  	"image_position" "enum_group_blocks_image_with_text_image_position" DEFAULT 'left',
  	"text" jsonb,
  	"block_name" varchar
  );
  
  CREATE TABLE "_group_v_blocks_image_with_text" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"title" varchar,
  	"title_color" "enum__group_v_blocks_image_with_text_title_color" DEFAULT 'base',
  	"background_color" "enum__group_v_blocks_image_with_text_background_color" DEFAULT 'none',
  	"image_id" uuid,
  	"image_position" "enum__group_v_blocks_image_with_text_image_position" DEFAULT 'left',
  	"text" jsonb,
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_image_with_text" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar,
  	"title_color" "enum_pages_blocks_image_with_text_title_color" DEFAULT 'base',
  	"background_color" "enum_pages_blocks_image_with_text_background_color" DEFAULT 'none',
  	"image_id" uuid,
  	"image_position" "enum_pages_blocks_image_with_text_image_position" DEFAULT 'left',
  	"text" jsonb,
  	"block_name" varchar
  );
  
  CREATE TABLE "_pages_v_blocks_image_with_text" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"title" varchar,
  	"title_color" "enum__pages_v_blocks_image_with_text_title_color" DEFAULT 'base',
  	"background_color" "enum__pages_v_blocks_image_with_text_background_color" DEFAULT 'none',
  	"image_id" uuid,
  	"image_position" "enum__pages_v_blocks_image_with_text_image_position" DEFAULT 'left',
  	"text" jsonb,
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-06-14T08:49:19.434Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-06-14T08:49:19.726Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-07-11T08:49:19.782Z';
  ALTER TABLE "parish_blocks_image_with_text" ADD CONSTRAINT "parish_blocks_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 "parish_blocks_image_with_text" ADD CONSTRAINT "parish_blocks_image_with_text_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_parish_v_blocks_image_with_text" ADD CONSTRAINT "_parish_v_blocks_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 "_parish_v_blocks_image_with_text" ADD CONSTRAINT "_parish_v_blocks_image_with_text_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_parish_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "group_blocks_image_with_text" ADD CONSTRAINT "group_blocks_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 "group_blocks_image_with_text" ADD CONSTRAINT "group_blocks_image_with_text_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_group_v_blocks_image_with_text" ADD CONSTRAINT "_group_v_blocks_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 "_group_v_blocks_image_with_text" ADD CONSTRAINT "_group_v_blocks_image_with_text_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_group_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_image_with_text" ADD CONSTRAINT "pages_blocks_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_image_with_text" ADD CONSTRAINT "pages_blocks_image_with_text_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_pages_v_blocks_image_with_text" ADD CONSTRAINT "_pages_v_blocks_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_v_blocks_image_with_text" ADD CONSTRAINT "_pages_v_blocks_image_with_text_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_pages_v"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "parish_blocks_image_with_text_order_idx" ON "parish_blocks_image_with_text" USING btree ("_order");
  CREATE INDEX "parish_blocks_image_with_text_parent_id_idx" ON "parish_blocks_image_with_text" USING btree ("_parent_id");
  CREATE INDEX "parish_blocks_image_with_text_path_idx" ON "parish_blocks_image_with_text" USING btree ("_path");
  CREATE INDEX "parish_blocks_image_with_text_image_idx" ON "parish_blocks_image_with_text" USING btree ("image_id");
  CREATE INDEX "_parish_v_blocks_image_with_text_order_idx" ON "_parish_v_blocks_image_with_text" USING btree ("_order");
  CREATE INDEX "_parish_v_blocks_image_with_text_parent_id_idx" ON "_parish_v_blocks_image_with_text" USING btree ("_parent_id");
  CREATE INDEX "_parish_v_blocks_image_with_text_path_idx" ON "_parish_v_blocks_image_with_text" USING btree ("_path");
  CREATE INDEX "_parish_v_blocks_image_with_text_image_idx" ON "_parish_v_blocks_image_with_text" USING btree ("image_id");
  CREATE INDEX "group_blocks_image_with_text_order_idx" ON "group_blocks_image_with_text" USING btree ("_order");
  CREATE INDEX "group_blocks_image_with_text_parent_id_idx" ON "group_blocks_image_with_text" USING btree ("_parent_id");
  CREATE INDEX "group_blocks_image_with_text_path_idx" ON "group_blocks_image_with_text" USING btree ("_path");
  CREATE INDEX "group_blocks_image_with_text_image_idx" ON "group_blocks_image_with_text" USING btree ("image_id");
  CREATE INDEX "_group_v_blocks_image_with_text_order_idx" ON "_group_v_blocks_image_with_text" USING btree ("_order");
  CREATE INDEX "_group_v_blocks_image_with_text_parent_id_idx" ON "_group_v_blocks_image_with_text" USING btree ("_parent_id");
  CREATE INDEX "_group_v_blocks_image_with_text_path_idx" ON "_group_v_blocks_image_with_text" USING btree ("_path");
  CREATE INDEX "_group_v_blocks_image_with_text_image_idx" ON "_group_v_blocks_image_with_text" USING btree ("image_id");
  CREATE INDEX "pages_blocks_image_with_text_order_idx" ON "pages_blocks_image_with_text" USING btree ("_order");
  CREATE INDEX "pages_blocks_image_with_text_parent_id_idx" ON "pages_blocks_image_with_text" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_image_with_text_path_idx" ON "pages_blocks_image_with_text" USING btree ("_path");
  CREATE INDEX "pages_blocks_image_with_text_image_idx" ON "pages_blocks_image_with_text" USING btree ("image_id");
  CREATE INDEX "_pages_v_blocks_image_with_text_order_idx" ON "_pages_v_blocks_image_with_text" USING btree ("_order");
  CREATE INDEX "_pages_v_blocks_image_with_text_parent_id_idx" ON "_pages_v_blocks_image_with_text" USING btree ("_parent_id");
  CREATE INDEX "_pages_v_blocks_image_with_text_path_idx" ON "_pages_v_blocks_image_with_text" USING btree ("_path");
  CREATE INDEX "_pages_v_blocks_image_with_text_image_idx" ON "_pages_v_blocks_image_with_text" USING btree ("image_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "parish_blocks_image_with_text" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_parish_v_blocks_image_with_text" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "group_blocks_image_with_text" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_group_v_blocks_image_with_text" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "pages_blocks_image_with_text" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_pages_v_blocks_image_with_text" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "parish_blocks_image_with_text" CASCADE;
  DROP TABLE "_parish_v_blocks_image_with_text" CASCADE;
  DROP TABLE "group_blocks_image_with_text" CASCADE;
  DROP TABLE "_group_v_blocks_image_with_text" CASCADE;
  DROP TABLE "pages_blocks_image_with_text" CASCADE;
  DROP TABLE "_pages_v_blocks_image_with_text" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-06-14T07:26:50.172Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-06-14T07:26:50.530Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-07-11T07:26:50.607Z';
  DROP TYPE "public"."enum_parish_blocks_image_with_text_title_color";
  DROP TYPE "public"."enum_parish_blocks_image_with_text_background_color";
  DROP TYPE "public"."enum_parish_blocks_image_with_text_image_position";
  DROP TYPE "public"."enum__parish_v_blocks_image_with_text_title_color";
  DROP TYPE "public"."enum__parish_v_blocks_image_with_text_background_color";
  DROP TYPE "public"."enum__parish_v_blocks_image_with_text_image_position";
  DROP TYPE "public"."enum_group_blocks_image_with_text_title_color";
  DROP TYPE "public"."enum_group_blocks_image_with_text_background_color";
  DROP TYPE "public"."enum_group_blocks_image_with_text_image_position";
  DROP TYPE "public"."enum__group_v_blocks_image_with_text_title_color";
  DROP TYPE "public"."enum__group_v_blocks_image_with_text_background_color";
  DROP TYPE "public"."enum__group_v_blocks_image_with_text_image_position";
  DROP TYPE "public"."enum_pages_blocks_image_with_text_title_color";
  DROP TYPE "public"."enum_pages_blocks_image_with_text_background_color";
  DROP TYPE "public"."enum_pages_blocks_image_with_text_image_position";
  DROP TYPE "public"."enum__pages_v_blocks_image_with_text_title_color";
  DROP TYPE "public"."enum__pages_v_blocks_image_with_text_background_color";
  DROP TYPE "public"."enum__pages_v_blocks_image_with_text_image_position";`)
}
