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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TABLE "group_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 "_group_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-06-14T07:26:50.172Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-06-14T07:26:50.530Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-07-11T07:26:50.607Z';
  ALTER TABLE "group_blocks_contact_person_block" ADD CONSTRAINT "group_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 "group_blocks_contact_person_block" ADD CONSTRAINT "group_blocks_contact_person_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_group_v_blocks_contact_person_block" ADD CONSTRAINT "_group_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 "_group_v_blocks_contact_person_block" ADD CONSTRAINT "_group_v_blocks_contact_person_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_group_v"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "group_blocks_contact_person_block_order_idx" ON "group_blocks_contact_person_block" USING btree ("_order");
  CREATE INDEX "group_blocks_contact_person_block_parent_id_idx" ON "group_blocks_contact_person_block" USING btree ("_parent_id");
  CREATE INDEX "group_blocks_contact_person_block_path_idx" ON "group_blocks_contact_person_block" USING btree ("_path");
  CREATE INDEX "group_blocks_contact_person_block_contact_idx" ON "group_blocks_contact_person_block" USING btree ("contact_id");
  CREATE INDEX "_group_v_blocks_contact_person_block_order_idx" ON "_group_v_blocks_contact_person_block" USING btree ("_order");
  CREATE INDEX "_group_v_blocks_contact_person_block_parent_id_idx" ON "_group_v_blocks_contact_person_block" USING btree ("_parent_id");
  CREATE INDEX "_group_v_blocks_contact_person_block_path_idx" ON "_group_v_blocks_contact_person_block" USING btree ("_path");
  CREATE INDEX "_group_v_blocks_contact_person_block_contact_idx" ON "_group_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 "group_blocks_contact_person_block" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_group_v_blocks_contact_person_block" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "group_blocks_contact_person_block" CASCADE;
  DROP TABLE "_group_v_blocks_contact_person_block" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-06-14T11:32:58.445Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-06-14T11:32:58.773Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-07-09T11:32:58.852Z';`)
}
