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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TABLE "blog_blocks_youtube" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"youtube_id" varchar NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE "group_blocks_youtube" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"youtube_id" varchar NOT NULL,
  	"block_name" varchar
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2025-11-23T15:05:28.806Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2025-11-23T15:05:28.910Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2025-12-18T15:05:28.976Z';
  ALTER TABLE "blog_blocks_youtube" ADD CONSTRAINT "blog_blocks_youtube_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."blog"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "group_blocks_youtube" ADD CONSTRAINT "group_blocks_youtube_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "blog_blocks_youtube_order_idx" ON "blog_blocks_youtube" USING btree ("_order");
  CREATE INDEX "blog_blocks_youtube_parent_id_idx" ON "blog_blocks_youtube" USING btree ("_parent_id");
  CREATE INDEX "blog_blocks_youtube_path_idx" ON "blog_blocks_youtube" USING btree ("_path");
  CREATE INDEX "group_blocks_youtube_order_idx" ON "group_blocks_youtube" USING btree ("_order");
  CREATE INDEX "group_blocks_youtube_parent_id_idx" ON "group_blocks_youtube" USING btree ("_parent_id");
  CREATE INDEX "group_blocks_youtube_path_idx" ON "group_blocks_youtube" USING btree ("_path");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "blog_blocks_youtube" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "group_blocks_youtube" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "blog_blocks_youtube" CASCADE;
  DROP TABLE "group_blocks_youtube" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2025-09-21T07:52:17.724Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2025-09-21T07:52:17.817Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2025-10-15T07:52:17.878Z';`)
}
