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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "employees" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "testimony" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "page_blocks_content" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "page_blocks_title" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "page_blocks_testimony" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "page" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "employees" CASCADE;
  DROP TABLE "testimony" CASCADE;
  DROP TABLE "page_blocks_content" CASCADE;
  DROP TABLE "page_blocks_title" CASCADE;
  DROP TABLE "page_blocks_testimony" CASCADE;
  DROP TABLE "page" CASCADE;
  ALTER TABLE "parish_rels" DROP CONSTRAINT IF EXISTS "parish_rels_employees_fk";
  
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT IF EXISTS "payload_locked_documents_rels_employees_fk";
  
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT IF EXISTS "payload_locked_documents_rels_testimony_fk";
  
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT IF EXISTS "payload_locked_documents_rels_page_fk";
  
  DROP INDEX IF EXISTS "parish_rels_employees_id_idx";
  DROP INDEX IF EXISTS "payload_locked_documents_rels_employees_id_idx";
  DROP INDEX IF EXISTS "payload_locked_documents_rels_testimony_id_idx";
  DROP INDEX IF EXISTS "payload_locked_documents_rels_page_id_idx";
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2025-03-02T08:36:52.653Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2025-03-02T08:36:52.738Z';
  ALTER TABLE "parish_rels" DROP COLUMN IF EXISTS "employees_id";
  ALTER TABLE "documents" DROP COLUMN IF EXISTS "name";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN IF EXISTS "employees_id";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN IF EXISTS "testimony_id";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN IF EXISTS "page_id";
  DROP TYPE "public"."enum_testimony_category";`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   CREATE TYPE "public"."enum_testimony_category" AS ENUM('EUCHARIST');
  CREATE TABLE IF NOT EXISTS "employees" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"photo_id" uuid,
  	"name" varchar NOT NULL,
  	"occupation" varchar NOT NULL,
  	"email" varchar,
  	"telephone" varchar,
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
  );
  
  CREATE TABLE IF NOT EXISTS "testimony" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"testimony" varchar NOT NULL,
  	"name" varchar NOT NULL,
  	"occupation" varchar,
  	"category" "enum_testimony_category" NOT NULL,
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
  );
  
  CREATE TABLE IF NOT EXISTS "page_blocks_content" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"content" jsonb NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "page_blocks_title" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "page_blocks_testimony" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"testimony_id" uuid NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "page" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"title" varchar NOT NULL,
  	"slug" varchar NOT NULL,
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2025-02-09T10:47:41.631Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2025-02-09T10:47:41.722Z';
  ALTER TABLE "parish_rels" ADD COLUMN "employees_id" uuid;
  ALTER TABLE "documents" ADD COLUMN "name" varchar NOT NULL;
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "employees_id" uuid;
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "testimony_id" uuid;
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "page_id" uuid;
  DO $$ BEGIN
   ALTER TABLE "employees" ADD CONSTRAINT "employees_photo_id_media_id_fk" FOREIGN KEY ("photo_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "page_blocks_content" ADD CONSTRAINT "page_blocks_content_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."page"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "page_blocks_title" ADD CONSTRAINT "page_blocks_title_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."page"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "page_blocks_testimony" ADD CONSTRAINT "page_blocks_testimony_testimony_id_testimony_id_fk" FOREIGN KEY ("testimony_id") REFERENCES "public"."testimony"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "page_blocks_testimony" ADD CONSTRAINT "page_blocks_testimony_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."page"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  CREATE INDEX IF NOT EXISTS "employees_photo_idx" ON "employees" USING btree ("photo_id");
  CREATE INDEX IF NOT EXISTS "employees_updated_at_idx" ON "employees" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "employees_created_at_idx" ON "employees" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "testimony_updated_at_idx" ON "testimony" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "testimony_created_at_idx" ON "testimony" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "page_blocks_content_order_idx" ON "page_blocks_content" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "page_blocks_content_parent_id_idx" ON "page_blocks_content" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "page_blocks_content_path_idx" ON "page_blocks_content" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "page_blocks_title_order_idx" ON "page_blocks_title" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "page_blocks_title_parent_id_idx" ON "page_blocks_title" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "page_blocks_title_path_idx" ON "page_blocks_title" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "page_blocks_testimony_order_idx" ON "page_blocks_testimony" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "page_blocks_testimony_parent_id_idx" ON "page_blocks_testimony" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "page_blocks_testimony_path_idx" ON "page_blocks_testimony" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "page_blocks_testimony_testimony_idx" ON "page_blocks_testimony" USING btree ("testimony_id");
  CREATE INDEX IF NOT EXISTS "page_updated_at_idx" ON "page" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "page_created_at_idx" ON "page" USING btree ("created_at");
  DO $$ BEGIN
   ALTER TABLE "parish_rels" ADD CONSTRAINT "parish_rels_employees_fk" FOREIGN KEY ("employees_id") REFERENCES "public"."employees"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_employees_fk" FOREIGN KEY ("employees_id") REFERENCES "public"."employees"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_testimony_fk" FOREIGN KEY ("testimony_id") REFERENCES "public"."testimony"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_page_fk" FOREIGN KEY ("page_id") REFERENCES "public"."page"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  CREATE INDEX IF NOT EXISTS "parish_rels_employees_id_idx" ON "parish_rels" USING btree ("employees_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_employees_id_idx" ON "payload_locked_documents_rels" USING btree ("employees_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_testimony_id_idx" ON "payload_locked_documents_rels" USING btree ("testimony_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_page_id_idx" ON "payload_locked_documents_rels" USING btree ("page_id");`)
}
