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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_video_embed_block_video_source" AS ENUM('youtube', 'vimeo', 'custom');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_video_embed_block_aspect_ratio" AS ENUM('16:9', '4:3', '1:1', '9:16');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_video_embed_block_max_width" AS ENUM('full', 'large', 'medium', 'small');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_video_embed_block_style_alignment" AS ENUM('left', 'center', 'right');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_video_embed_block_style_border_radius" AS ENUM('none', 'sm', 'md', 'lg');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

CREATE TABLE IF NOT EXISTS "pages_blocks_video_embed_block" (
  "_order" integer NOT NULL,
  "_parent_id" integer NOT NULL,
  "_path" text NOT NULL,
  "id" varchar PRIMARY KEY NOT NULL,
  "video_source" "enum_pages_blocks_video_embed_block_video_source" DEFAULT 'youtube' NOT NULL,
  "youtube_url" varchar,
  "vimeo_url" varchar,
  "custom_url" varchar,
  "thumbnail_id" integer,
  "privacy_mode" boolean DEFAULT true,
  "lazy_load" boolean DEFAULT true,
  "aspect_ratio" "enum_pages_blocks_video_embed_block_aspect_ratio" DEFAULT '16:9',
  "max_width" "enum_pages_blocks_video_embed_block_max_width" DEFAULT 'large',
  "playback_options_autoplay" boolean DEFAULT false,
  "playback_options_muted" boolean DEFAULT false,
  "playback_options_loop" boolean DEFAULT false,
  "playback_options_show_controls" boolean DEFAULT true,
  "playback_options_start_time" numeric,
  "style_alignment" "enum_pages_blocks_video_embed_block_style_alignment" DEFAULT 'center',
  "style_border_radius" "enum_pages_blocks_video_embed_block_style_border_radius" DEFAULT 'md',
  "style_shadow" boolean DEFAULT true,
  "block_name" varchar
);

CREATE TABLE IF NOT EXISTS "pages_blocks_video_embed_block_locales" (
  "title" varchar,
  "caption" varchar,
  "id" serial PRIMARY KEY NOT NULL,
  "_locale" "_locales" NOT NULL,
  "_parent_id" varchar NOT NULL
);

DO $$ BEGIN
  ALTER TABLE "pages_blocks_video_embed_block" ADD CONSTRAINT "pages_blocks_video_embed_block_thumbnail_id_media_id_fk" FOREIGN KEY ("thumbnail_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_video_embed_block" ADD CONSTRAINT "pages_blocks_video_embed_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_video_embed_block_locales" ADD CONSTRAINT "pages_blocks_video_embed_block_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_video_embed_block"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

CREATE INDEX IF NOT EXISTS "pages_blocks_video_embed_block_order_idx" ON "pages_blocks_video_embed_block" USING btree ("_order");
CREATE INDEX IF NOT EXISTS "pages_blocks_video_embed_block_parent_id_idx" ON "pages_blocks_video_embed_block" USING btree ("_parent_id");
CREATE INDEX IF NOT EXISTS "pages_blocks_video_embed_block_path_idx" ON "pages_blocks_video_embed_block" USING btree ("_path");
CREATE INDEX IF NOT EXISTS "pages_blocks_video_embed_block_thumbnail_idx" ON "pages_blocks_video_embed_block" USING btree ("thumbnail_id");
CREATE UNIQUE INDEX IF NOT EXISTS "pages_blocks_video_embed_block_locales_locale_parent_id_uniq" ON "pages_blocks_video_embed_block_locales" USING btree ("_locale","_parent_id");
  `)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  // No-op: data-preserving migration for the video embed block tables.
}
