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_title_size" AS ENUM('xl', 'lg', 'md', 'sm');
  CREATE TYPE "public"."enum_parish_blocks_title_align" AS ENUM('left', 'center');
  CREATE TYPE "public"."enum__parish_v_blocks_title_size" AS ENUM('xl', 'lg', 'md', 'sm');
  CREATE TYPE "public"."enum__parish_v_blocks_title_align" AS ENUM('left', 'center');
  CREATE TABLE "parish_blocks_title" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar,
  	"subtitle" varchar,
  	"size" "enum_parish_blocks_title_size" DEFAULT 'lg',
  	"align" "enum_parish_blocks_title_align" DEFAULT 'left',
  	"block_name" varchar
  );
  
  CREATE TABLE "_parish_v_blocks_title" (
  	"_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,
  	"subtitle" varchar,
  	"size" "enum__parish_v_blocks_title_size" DEFAULT 'lg',
  	"align" "enum__parish_v_blocks_title_align" DEFAULT 'left',
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-12T11:52:47.682Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-12T11:52:47.973Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-10T11:52:48.026Z';
  ALTER TABLE "parish_blocks_title" ADD CONSTRAINT "parish_blocks_title_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_parish_v_blocks_title" ADD CONSTRAINT "_parish_v_blocks_title_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_parish_v"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "parish_blocks_title_order_idx" ON "parish_blocks_title" USING btree ("_order");
  CREATE INDEX "parish_blocks_title_parent_id_idx" ON "parish_blocks_title" USING btree ("_parent_id");
  CREATE INDEX "parish_blocks_title_path_idx" ON "parish_blocks_title" USING btree ("_path");
  CREATE INDEX "_parish_v_blocks_title_order_idx" ON "_parish_v_blocks_title" USING btree ("_order");
  CREATE INDEX "_parish_v_blocks_title_parent_id_idx" ON "_parish_v_blocks_title" USING btree ("_parent_id");
  CREATE INDEX "_parish_v_blocks_title_path_idx" ON "_parish_v_blocks_title" USING btree ("_path");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "parish_blocks_title" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_parish_v_blocks_title" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "parish_blocks_title" CASCADE;
  DROP TABLE "_parish_v_blocks_title" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-12T11:40:57.328Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-12T11:40:57.621Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-10T11:40:57.679Z';
  DROP TYPE "public"."enum_parish_blocks_title_size";
  DROP TYPE "public"."enum_parish_blocks_title_align";
  DROP TYPE "public"."enum__parish_v_blocks_title_size";
  DROP TYPE "public"."enum__parish_v_blocks_title_align";`)
}
