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_collaps_color_style" AS ENUM('default', 'soft', 'off-white', 'contrast');
  CREATE TYPE "public"."enum__collaps_v_color_style" AS ENUM('default', 'soft', 'off-white', 'contrast');
  CREATE TABLE "collaps" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar,
  	"text" varchar,
  	"image_id" uuid,
  	"content" jsonb,
  	"color_style" "enum_collaps_color_style" DEFAULT 'default',
  	"block_name" varchar
  );
  
  CREATE TABLE "_collaps_v" (
  	"_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,
  	"text" varchar,
  	"image_id" uuid,
  	"content" jsonb,
  	"color_style" "enum__collaps_v_color_style" DEFAULT 'default',
  	"_uuid" varchar,
  	"block_name" varchar
  );
  
  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';
  ALTER TABLE "collaps" ADD CONSTRAINT "collaps_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "collaps" ADD CONSTRAINT "collaps_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_collaps_v" ADD CONSTRAINT "_collaps_v_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "_collaps_v" ADD CONSTRAINT "_collaps_v_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."_pages_v"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "collaps_order_idx" ON "collaps" USING btree ("_order");
  CREATE INDEX "collaps_parent_id_idx" ON "collaps" USING btree ("_parent_id");
  CREATE INDEX "collaps_path_idx" ON "collaps" USING btree ("_path");
  CREATE INDEX "collaps_image_idx" ON "collaps" USING btree ("image_id");
  CREATE INDEX "_collaps_v_order_idx" ON "_collaps_v" USING btree ("_order");
  CREATE INDEX "_collaps_v_parent_id_idx" ON "_collaps_v" USING btree ("_parent_id");
  CREATE INDEX "_collaps_v_path_idx" ON "_collaps_v" USING btree ("_path");
  CREATE INDEX "_collaps_v_image_idx" ON "_collaps_v" USING btree ("image_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "collaps" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "_collaps_v" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "collaps" CASCADE;
  DROP TABLE "_collaps_v" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-03-15T11:02:35.690Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-03-15T11:02:35.969Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-04-10T10:02:36.027Z';
  DROP TYPE "public"."enum_collaps_color_style";
  DROP TYPE "public"."enum__collaps_v_color_style";`)
}
