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_menu_blocks_simple_item_type" AS ENUM('default', 'button');
  CREATE TABLE IF NOT EXISTS "menu_blocks_simple_item" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"text" varchar NOT NULL,
  	"href" varchar NOT NULL,
  	"type" "enum_menu_blocks_simple_item_type" DEFAULT 'default' NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "menu_blocks_mega_menu_groups_items" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar NOT NULL,
  	"description" varchar NOT NULL,
  	"href" varchar NOT NULL
  );
  
  CREATE TABLE IF NOT EXISTS "menu_blocks_mega_menu_groups" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar NOT NULL
  );
  
  CREATE TABLE IF NOT EXISTS "menu_blocks_mega_menu" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"text" varchar NOT NULL,
  	"quote" varchar NOT NULL,
  	"source" varchar NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "menu" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"updated_at" timestamp(3) with time zone,
  	"created_at" timestamp(3) with time zone
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2025-08-24T14:31:15.010Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2025-08-24T14:31:15.092Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2025-09-17T14:31:15.112Z';
  DO $$ BEGIN
   ALTER TABLE "menu_blocks_simple_item" ADD CONSTRAINT "menu_blocks_simple_item_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."menu"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "menu_blocks_mega_menu_groups_items" ADD CONSTRAINT "menu_blocks_mega_menu_groups_items_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."menu_blocks_mega_menu_groups"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "menu_blocks_mega_menu_groups" ADD CONSTRAINT "menu_blocks_mega_menu_groups_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."menu_blocks_mega_menu"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "menu_blocks_mega_menu" ADD CONSTRAINT "menu_blocks_mega_menu_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."menu"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  CREATE INDEX IF NOT EXISTS "menu_blocks_simple_item_order_idx" ON "menu_blocks_simple_item" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "menu_blocks_simple_item_parent_id_idx" ON "menu_blocks_simple_item" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "menu_blocks_simple_item_path_idx" ON "menu_blocks_simple_item" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "menu_blocks_mega_menu_groups_items_order_idx" ON "menu_blocks_mega_menu_groups_items" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "menu_blocks_mega_menu_groups_items_parent_id_idx" ON "menu_blocks_mega_menu_groups_items" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "menu_blocks_mega_menu_groups_order_idx" ON "menu_blocks_mega_menu_groups" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "menu_blocks_mega_menu_groups_parent_id_idx" ON "menu_blocks_mega_menu_groups" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "menu_blocks_mega_menu_order_idx" ON "menu_blocks_mega_menu" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "menu_blocks_mega_menu_parent_id_idx" ON "menu_blocks_mega_menu" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "menu_blocks_mega_menu_path_idx" ON "menu_blocks_mega_menu" USING btree ("_path");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "menu_blocks_simple_item" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "menu_blocks_mega_menu_groups_items" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "menu_blocks_mega_menu_groups" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "menu_blocks_mega_menu" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "menu" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "menu_blocks_simple_item" CASCADE;
  DROP TABLE "menu_blocks_mega_menu_groups_items" CASCADE;
  DROP TABLE "menu_blocks_mega_menu_groups" CASCADE;
  DROP TABLE "menu_blocks_mega_menu" CASCADE;
  DROP TABLE "menu" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2025-06-15T10:51:24.116Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2025-06-15T10:51:24.203Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2025-07-08T10:51:24.219Z';
  DROP TYPE "public"."enum_menu_blocks_simple_item_type";`)
}
