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_group_blocks_title_size" AS ENUM('xl', 'lg', 'md', 'sm');
  CREATE TYPE "public"."enum_group_blocks_title_align" AS ENUM('left', 'center');
  CREATE TYPE "public"."enum_group_blocks_title_color" AS ENUM('base', 'shade1', 'shade2', 'shade3', 'contrast', 'contrastShade1');
  CREATE TYPE "public"."enum__group_v_blocks_title_size" AS ENUM('xl', 'lg', 'md', 'sm');
  CREATE TYPE "public"."enum__group_v_blocks_title_align" AS ENUM('left', 'center');
  CREATE TYPE "public"."enum__group_v_blocks_title_color" AS ENUM('base', 'shade1', 'shade2', 'shade3', 'contrast', 'contrastShade1');
  CREATE TABLE "group_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_group_blocks_title_size" DEFAULT 'lg',
  	"align" "enum_group_blocks_title_align" DEFAULT 'left',
  	"color" "enum_group_blocks_title_color" DEFAULT 'base',
  	"block_name" varchar
  );
  
  CREATE TABLE "_group_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__group_v_blocks_title_size" DEFAULT 'lg',
  	"align" "enum__group_v_blocks_title_align" DEFAULT 'left',
  	"color" "enum__group_v_blocks_title_color" DEFAULT 'base',
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-19T12:14:50.471Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-19T12:14:50.803Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-16T12:14:50.859Z';
  ALTER TABLE "group_blocks_title" ADD CONSTRAINT "group_blocks_title_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_group_v_blocks_title" ADD CONSTRAINT "_group_v_blocks_title_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_group_v"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "group_blocks_title_order_idx" ON "group_blocks_title" USING btree ("_order");
  CREATE INDEX "group_blocks_title_parent_id_idx" ON "group_blocks_title" USING btree ("_parent_id");
  CREATE INDEX "group_blocks_title_path_idx" ON "group_blocks_title" USING btree ("_path");
  CREATE INDEX "_group_v_blocks_title_order_idx" ON "_group_v_blocks_title" USING btree ("_order");
  CREATE INDEX "_group_v_blocks_title_parent_id_idx" ON "_group_v_blocks_title" USING btree ("_parent_id");
  CREATE INDEX "_group_v_blocks_title_path_idx" ON "_group_v_blocks_title" USING btree ("_path");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "group_blocks_title" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_group_v_blocks_title" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "group_blocks_title" CASCADE;
  DROP TABLE "_group_v_blocks_title" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-19T11:54:45.762Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-19T11:54:46.057Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-16T11:54:46.114Z';
  DROP TYPE "public"."enum_group_blocks_title_size";
  DROP TYPE "public"."enum_group_blocks_title_align";
  DROP TYPE "public"."enum_group_blocks_title_color";
  DROP TYPE "public"."enum__group_v_blocks_title_size";
  DROP TYPE "public"."enum__group_v_blocks_title_align";
  DROP TYPE "public"."enum__group_v_blocks_title_color";`)
}
