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_text_width" AS ENUM('1/2', '3/4');
  CREATE TABLE "parish_blocks_text" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"content" jsonb NOT NULL,
  	"content_html" varchar,
  	"width" "enum_parish_blocks_text_width" DEFAULT '1/2' NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE "parish_blocks_document" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"file_id" uuid NOT NULL,
  	"button" varchar DEFAULT 'Download Flyer' NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE "parish_blocks_donation" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE "parish_blocks_youtube" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"youtube_id" varchar NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE "parish_blocks_donation_appeal" (
  	"_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-01-11T10:35:28.881Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-01-11T10:35:28.965Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-02-05T10:35:29.018Z';
  ALTER TABLE "parish_blocks_text" ADD CONSTRAINT "parish_blocks_text_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "parish_blocks_document" ADD CONSTRAINT "parish_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 "parish_blocks_document" ADD CONSTRAINT "parish_blocks_document_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "parish_blocks_donation" ADD CONSTRAINT "parish_blocks_donation_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "parish_blocks_youtube" ADD CONSTRAINT "parish_blocks_youtube_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "parish_blocks_donation_appeal" ADD CONSTRAINT "parish_blocks_donation_appeal_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "parish_blocks_text_order_idx" ON "parish_blocks_text" USING btree ("_order");
  CREATE INDEX "parish_blocks_text_parent_id_idx" ON "parish_blocks_text" USING btree ("_parent_id");
  CREATE INDEX "parish_blocks_text_path_idx" ON "parish_blocks_text" USING btree ("_path");
  CREATE INDEX "parish_blocks_document_order_idx" ON "parish_blocks_document" USING btree ("_order");
  CREATE INDEX "parish_blocks_document_parent_id_idx" ON "parish_blocks_document" USING btree ("_parent_id");
  CREATE INDEX "parish_blocks_document_path_idx" ON "parish_blocks_document" USING btree ("_path");
  CREATE INDEX "parish_blocks_document_file_idx" ON "parish_blocks_document" USING btree ("file_id");
  CREATE INDEX "parish_blocks_donation_order_idx" ON "parish_blocks_donation" USING btree ("_order");
  CREATE INDEX "parish_blocks_donation_parent_id_idx" ON "parish_blocks_donation" USING btree ("_parent_id");
  CREATE INDEX "parish_blocks_donation_path_idx" ON "parish_blocks_donation" USING btree ("_path");
  CREATE INDEX "parish_blocks_youtube_order_idx" ON "parish_blocks_youtube" USING btree ("_order");
  CREATE INDEX "parish_blocks_youtube_parent_id_idx" ON "parish_blocks_youtube" USING btree ("_parent_id");
  CREATE INDEX "parish_blocks_youtube_path_idx" ON "parish_blocks_youtube" USING btree ("_path");
  CREATE INDEX "parish_blocks_donation_appeal_order_idx" ON "parish_blocks_donation_appeal" USING btree ("_order");
  CREATE INDEX "parish_blocks_donation_appeal_parent_id_idx" ON "parish_blocks_donation_appeal" USING btree ("_parent_id");
  CREATE INDEX "parish_blocks_donation_appeal_path_idx" ON "parish_blocks_donation_appeal" USING btree ("_path");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "parish_blocks_text" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "parish_blocks_document" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "parish_blocks_donation" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "parish_blocks_youtube" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "parish_blocks_donation_appeal" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "parish_blocks_text" CASCADE;
  DROP TABLE "parish_blocks_document" CASCADE;
  DROP TABLE "parish_blocks_donation" CASCADE;
  DROP TABLE "parish_blocks_youtube" CASCADE;
  DROP TABLE "parish_blocks_donation_appeal" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-01-11T08:54:45.107Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-01-11T08:54:45.188Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-02-05T08:54:45.242Z';
  DROP TYPE "public"."enum_parish_blocks_text_width";`)
}
