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_blog_status" AS ENUM('draft', 'published');
  CREATE TYPE "public"."enum__blog_v_blocks_text_width" AS ENUM('1/2', '3/4');
  CREATE TYPE "public"."enum__blog_v_version_status" AS ENUM('draft', 'published');
  CREATE TABLE "_blog_v_blocks_text" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"content" jsonb,
  	"width" "enum__blog_v_blocks_text_width" DEFAULT '1/2',
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "_blog_v_blocks_document" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"file_id" uuid,
  	"button" varchar DEFAULT 'Download Flyer',
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "_blog_v_blocks_donation" (
  	"_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 "_blog_v_blocks_contactform" (
  	"_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 DEFAULT 'Ich bin dabei!',
  	"description" varchar DEFAULT 'Um dich anzumelden oder uns zu unterstützen, fülle bitte das Kontaktformular aus. Wir freuen uns sehr, dass du Teil unserer Gemeinschaft bist und mit deinem Engagement dazu beiträgst, unsere Ziele zu erreichen. Solltest du Fragen haben oder weitere Informationen benötigen, zögere nicht, uns zu kontaktieren – wir sind gerne für dich da!',
  	"email" varchar DEFAULT 'kontakt@mutter-teresa-chemnitz.de',
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "_blog_v_blocks_gallery_items" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"photo_id" uuid,
  	"_uuid" varchar
  );
  
  CREATE TABLE "_blog_v_blocks_gallery" (
  	"_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 "_blog_v_blocks_youtube" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"youtube_id" varchar,
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "_blog_v_blocks_button" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"text" varchar,
  	"url" varchar,
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "_blog_v" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"parent_id" uuid,
  	"version_photo_id" uuid,
  	"version_title" varchar,
  	"version_content_excerpt" varchar,
  	"version_configuration_show_on_frontpage" boolean DEFAULT true,
  	"version_configuration_display_from_date" timestamp(3) with time zone,
  	"version_configuration_display_till_date" timestamp(3) with time zone,
  	"version_updated_at" timestamp(3) with time zone,
  	"version_created_at" timestamp(3) with time zone,
  	"version__status" "enum__blog_v_version_status" DEFAULT 'draft',
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"latest" boolean,
  	"autosave" boolean
  );
  
  CREATE TABLE "_blog_v_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" uuid NOT NULL,
  	"path" varchar NOT NULL,
  	"parish_id" uuid
  );
  
  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 "blog_blocks_text" ALTER COLUMN "content" DROP NOT NULL;
  ALTER TABLE "blog_blocks_text" ALTER COLUMN "width" DROP NOT NULL;
  ALTER TABLE "blog_blocks_document" ALTER COLUMN "file_id" DROP NOT NULL;
  ALTER TABLE "blog_blocks_document" ALTER COLUMN "button" DROP NOT NULL;
  ALTER TABLE "blog_blocks_contactform" ALTER COLUMN "title" DROP NOT NULL;
  ALTER TABLE "blog_blocks_contactform" ALTER COLUMN "description" DROP NOT NULL;
  ALTER TABLE "blog_blocks_contactform" ALTER COLUMN "email" SET DEFAULT 'kontakt@mutter-teresa-chemnitz.de';
  ALTER TABLE "blog_blocks_contactform" ALTER COLUMN "email" DROP NOT NULL;
  ALTER TABLE "blog_blocks_gallery_items" ALTER COLUMN "photo_id" DROP NOT NULL;
  ALTER TABLE "blog_blocks_youtube" ALTER COLUMN "youtube_id" DROP NOT NULL;
  ALTER TABLE "blog_blocks_button" ALTER COLUMN "text" DROP NOT NULL;
  ALTER TABLE "blog_blocks_button" ALTER COLUMN "url" DROP NOT NULL;
  ALTER TABLE "blog" ALTER COLUMN "title" DROP NOT NULL;
  ALTER TABLE "blog" ALTER COLUMN "content_excerpt" DROP NOT NULL;
  ALTER TABLE "blog" ALTER COLUMN "configuration_show_on_frontpage" DROP NOT NULL;
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-04-09T13:38:00.592Z';
  ALTER TABLE "group_blocks_contactform" ALTER COLUMN "email" SET DEFAULT 'kontakt@mutter-teresa-chemnitz.de';
  ALTER TABLE "pages_blocks_contactform" ALTER COLUMN "email" SET DEFAULT 'kontakt@mutter-teresa-chemnitz.de';
  ALTER TABLE "blog" ADD COLUMN "_status" "enum_blog_status" DEFAULT 'draft';
  ALTER TABLE "_blog_v_blocks_text" ADD CONSTRAINT "_blog_v_blocks_text_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_blog_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_blog_v_blocks_document" ADD CONSTRAINT "_blog_v_blocks_document_file_id_documents_id_fk" FOREIGN KEY ("file_id") REFERENCES "public"."documents"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "_blog_v_blocks_document" ADD CONSTRAINT "_blog_v_blocks_document_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_blog_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_blog_v_blocks_donation" ADD CONSTRAINT "_blog_v_blocks_donation_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_blog_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_blog_v_blocks_contactform" ADD CONSTRAINT "_blog_v_blocks_contactform_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_blog_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_blog_v_blocks_gallery_items" ADD CONSTRAINT "_blog_v_blocks_gallery_items_photo_id_media_id_fk" FOREIGN KEY ("photo_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "_blog_v_blocks_gallery_items" ADD CONSTRAINT "_blog_v_blocks_gallery_items_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_blog_v_blocks_gallery"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_blog_v_blocks_gallery" ADD CONSTRAINT "_blog_v_blocks_gallery_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_blog_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_blog_v_blocks_youtube" ADD CONSTRAINT "_blog_v_blocks_youtube_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_blog_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_blog_v_blocks_button" ADD CONSTRAINT "_blog_v_blocks_button_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_blog_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_blog_v" ADD CONSTRAINT "_blog_v_parent_id_blog_id_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."blog"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "_blog_v" ADD CONSTRAINT "_blog_v_version_photo_id_media_id_fk" FOREIGN KEY ("version_photo_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "_blog_v_rels" ADD CONSTRAINT "_blog_v_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."_blog_v"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_blog_v_rels" ADD CONSTRAINT "_blog_v_rels_parish_fk" FOREIGN KEY ("parish_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "_blog_v_blocks_text_order_idx" ON "_blog_v_blocks_text" USING btree ("_order");
  CREATE INDEX "_blog_v_blocks_text_parent_id_idx" ON "_blog_v_blocks_text" USING btree ("_parent_id");
  CREATE INDEX "_blog_v_blocks_text_path_idx" ON "_blog_v_blocks_text" USING btree ("_path");
  CREATE INDEX "_blog_v_blocks_document_order_idx" ON "_blog_v_blocks_document" USING btree ("_order");
  CREATE INDEX "_blog_v_blocks_document_parent_id_idx" ON "_blog_v_blocks_document" USING btree ("_parent_id");
  CREATE INDEX "_blog_v_blocks_document_path_idx" ON "_blog_v_blocks_document" USING btree ("_path");
  CREATE INDEX "_blog_v_blocks_document_file_idx" ON "_blog_v_blocks_document" USING btree ("file_id");
  CREATE INDEX "_blog_v_blocks_donation_order_idx" ON "_blog_v_blocks_donation" USING btree ("_order");
  CREATE INDEX "_blog_v_blocks_donation_parent_id_idx" ON "_blog_v_blocks_donation" USING btree ("_parent_id");
  CREATE INDEX "_blog_v_blocks_donation_path_idx" ON "_blog_v_blocks_donation" USING btree ("_path");
  CREATE INDEX "_blog_v_blocks_contactform_order_idx" ON "_blog_v_blocks_contactform" USING btree ("_order");
  CREATE INDEX "_blog_v_blocks_contactform_parent_id_idx" ON "_blog_v_blocks_contactform" USING btree ("_parent_id");
  CREATE INDEX "_blog_v_blocks_contactform_path_idx" ON "_blog_v_blocks_contactform" USING btree ("_path");
  CREATE INDEX "_blog_v_blocks_gallery_items_order_idx" ON "_blog_v_blocks_gallery_items" USING btree ("_order");
  CREATE INDEX "_blog_v_blocks_gallery_items_parent_id_idx" ON "_blog_v_blocks_gallery_items" USING btree ("_parent_id");
  CREATE INDEX "_blog_v_blocks_gallery_items_photo_idx" ON "_blog_v_blocks_gallery_items" USING btree ("photo_id");
  CREATE INDEX "_blog_v_blocks_gallery_order_idx" ON "_blog_v_blocks_gallery" USING btree ("_order");
  CREATE INDEX "_blog_v_blocks_gallery_parent_id_idx" ON "_blog_v_blocks_gallery" USING btree ("_parent_id");
  CREATE INDEX "_blog_v_blocks_gallery_path_idx" ON "_blog_v_blocks_gallery" USING btree ("_path");
  CREATE INDEX "_blog_v_blocks_youtube_order_idx" ON "_blog_v_blocks_youtube" USING btree ("_order");
  CREATE INDEX "_blog_v_blocks_youtube_parent_id_idx" ON "_blog_v_blocks_youtube" USING btree ("_parent_id");
  CREATE INDEX "_blog_v_blocks_youtube_path_idx" ON "_blog_v_blocks_youtube" USING btree ("_path");
  CREATE INDEX "_blog_v_blocks_button_order_idx" ON "_blog_v_blocks_button" USING btree ("_order");
  CREATE INDEX "_blog_v_blocks_button_parent_id_idx" ON "_blog_v_blocks_button" USING btree ("_parent_id");
  CREATE INDEX "_blog_v_blocks_button_path_idx" ON "_blog_v_blocks_button" USING btree ("_path");
  CREATE INDEX "_blog_v_parent_idx" ON "_blog_v" USING btree ("parent_id");
  CREATE INDEX "_blog_v_version_version_photo_idx" ON "_blog_v" USING btree ("version_photo_id");
  CREATE INDEX "_blog_v_version_version_updated_at_idx" ON "_blog_v" USING btree ("version_updated_at");
  CREATE INDEX "_blog_v_version_version_created_at_idx" ON "_blog_v" USING btree ("version_created_at");
  CREATE INDEX "_blog_v_version_version__status_idx" ON "_blog_v" USING btree ("version__status");
  CREATE INDEX "_blog_v_created_at_idx" ON "_blog_v" USING btree ("created_at");
  CREATE INDEX "_blog_v_updated_at_idx" ON "_blog_v" USING btree ("updated_at");
  CREATE INDEX "_blog_v_latest_idx" ON "_blog_v" USING btree ("latest");
  CREATE INDEX "_blog_v_autosave_idx" ON "_blog_v" USING btree ("autosave");
  CREATE INDEX "_blog_v_rels_order_idx" ON "_blog_v_rels" USING btree ("order");
  CREATE INDEX "_blog_v_rels_parent_idx" ON "_blog_v_rels" USING btree ("parent_id");
  CREATE INDEX "_blog_v_rels_path_idx" ON "_blog_v_rels" USING btree ("path");
  CREATE INDEX "_blog_v_rels_parish_id_idx" ON "_blog_v_rels" USING btree ("parish_id");
  CREATE INDEX "blog__status_idx" ON "blog" USING btree ("_status");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "_blog_v_blocks_text" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_blog_v_blocks_document" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_blog_v_blocks_donation" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_blog_v_blocks_contactform" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_blog_v_blocks_gallery_items" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_blog_v_blocks_gallery" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_blog_v_blocks_youtube" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_blog_v_blocks_button" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_blog_v" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_blog_v_rels" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "_blog_v_blocks_text" CASCADE;
  DROP TABLE "_blog_v_blocks_document" CASCADE;
  DROP TABLE "_blog_v_blocks_donation" CASCADE;
  DROP TABLE "_blog_v_blocks_contactform" CASCADE;
  DROP TABLE "_blog_v_blocks_gallery_items" CASCADE;
  DROP TABLE "_blog_v_blocks_gallery" CASCADE;
  DROP TABLE "_blog_v_blocks_youtube" CASCADE;
  DROP TABLE "_blog_v_blocks_button" CASCADE;
  DROP TABLE "_blog_v" CASCADE;
  DROP TABLE "_blog_v_rels" CASCADE;
  DROP INDEX "blog__status_idx";
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-03-15T10:58:14.115Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-03-15T10:58:14.432Z';
  ALTER TABLE "blog_blocks_text" ALTER COLUMN "content" SET NOT NULL;
  ALTER TABLE "blog_blocks_text" ALTER COLUMN "width" SET NOT NULL;
  ALTER TABLE "blog_blocks_document" ALTER COLUMN "file_id" SET NOT NULL;
  ALTER TABLE "blog_blocks_document" ALTER COLUMN "button" SET NOT NULL;
  ALTER TABLE "blog_blocks_contactform" ALTER COLUMN "title" SET NOT NULL;
  ALTER TABLE "blog_blocks_contactform" ALTER COLUMN "description" SET NOT NULL;
  ALTER TABLE "blog_blocks_contactform" ALTER COLUMN "email" DROP DEFAULT;
  ALTER TABLE "blog_blocks_contactform" ALTER COLUMN "email" SET NOT NULL;
  ALTER TABLE "blog_blocks_gallery_items" ALTER COLUMN "photo_id" SET NOT NULL;
  ALTER TABLE "blog_blocks_youtube" ALTER COLUMN "youtube_id" SET NOT NULL;
  ALTER TABLE "blog_blocks_button" ALTER COLUMN "text" SET NOT NULL;
  ALTER TABLE "blog_blocks_button" ALTER COLUMN "url" SET NOT NULL;
  ALTER TABLE "blog" ALTER COLUMN "title" SET NOT NULL;
  ALTER TABLE "blog" ALTER COLUMN "content_excerpt" SET NOT NULL;
  ALTER TABLE "blog" ALTER COLUMN "configuration_show_on_frontpage" SET NOT NULL;
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-04-09T09:58:14.493Z';
  ALTER TABLE "group_blocks_contactform" ALTER COLUMN "email" DROP DEFAULT;
  ALTER TABLE "pages_blocks_contactform" ALTER COLUMN "email" DROP DEFAULT;
  ALTER TABLE "blog" DROP COLUMN "_status";
  DROP TYPE "public"."enum_blog_status";
  DROP TYPE "public"."enum__blog_v_blocks_text_width";
  DROP TYPE "public"."enum__blog_v_version_status";`)
}
