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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TABLE "pages_blocks_contact_person_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"contact_id" uuid,
  	"block_name" varchar
  );
  
  CREATE TABLE "_pages_v_blocks_contact_person_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"contact_id" uuid,
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-03-22T22:38:03.504Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-03-22T22:38:03.783Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-04-18T21:38:03.836Z';
  ALTER TABLE "pages_blocks_contact_person_block" ADD CONSTRAINT "pages_blocks_contact_person_block_contact_id_contact_person_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contact_person"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "pages_blocks_contact_person_block" ADD CONSTRAINT "pages_blocks_contact_person_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_pages_v_blocks_contact_person_block" ADD CONSTRAINT "_pages_v_blocks_contact_person_block_contact_id_contact_person_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contact_person"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "_pages_v_blocks_contact_person_block" ADD CONSTRAINT "_pages_v_blocks_contact_person_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_pages_v"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "pages_blocks_contact_person_block_order_idx" ON "pages_blocks_contact_person_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_contact_person_block_parent_id_idx" ON "pages_blocks_contact_person_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_contact_person_block_path_idx" ON "pages_blocks_contact_person_block" USING btree ("_path");
  CREATE INDEX "pages_blocks_contact_person_block_contact_idx" ON "pages_blocks_contact_person_block" USING btree ("contact_id");
  CREATE INDEX "_pages_v_blocks_contact_person_block_order_idx" ON "_pages_v_blocks_contact_person_block" USING btree ("_order");
  CREATE INDEX "_pages_v_blocks_contact_person_block_parent_id_idx" ON "_pages_v_blocks_contact_person_block" USING btree ("_parent_id");
  CREATE INDEX "_pages_v_blocks_contact_person_block_path_idx" ON "_pages_v_blocks_contact_person_block" USING btree ("_path");
  CREATE INDEX "_pages_v_blocks_contact_person_block_contact_idx" ON "_pages_v_blocks_contact_person_block" USING btree ("contact_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "pages_blocks_contact_person_block" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_pages_v_blocks_contact_person_block" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "pages_blocks_contact_person_block" CASCADE;
  DROP TABLE "_pages_v_blocks_contact_person_block" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-03-22T21:58:39.666Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-03-22T21:58:39.963Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-04-18T20:58:40.019Z';`)
}
