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

/**
 * Migration: Add BlogWoman Block Tables
 *
 * Creates the missing pages_blocks_* tables for BlogWoman blocks:
 * - favorites-block
 * - series-block
 * - series-detail-block
 * - featured-content-block
 */
export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
-- =====================================================
-- ENUM TYPES for BlogWoman Blocks
-- =====================================================

-- FavoritesBlock ENUMs
DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_favorites_block_category" AS ENUM('all', 'fashion', 'beauty', 'travel', 'tech', 'home');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_favorites_block_layout" AS ENUM('grid', 'list', 'carousel');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_favorites_block_columns" AS ENUM('2', '3', '4');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_favorites_block_background_color" AS ENUM('white', 'ivory', 'sand', 'light', 'dark');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

-- SeriesBlock ENUMs
DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_series_block_layout" AS ENUM('grid', 'list', 'featured');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_series_block_columns" AS ENUM('2', '3', '4');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_series_block_background_color" AS ENUM('white', 'ivory', 'sand', 'light', 'dark');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

-- SeriesDetailBlock ENUMs
DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_series_detail_block_layout" AS ENUM('full', 'compact');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

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

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

-- FeaturedContentBlock ENUMs
DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_featured_content_block_layout" AS ENUM('grid', 'carousel', 'list', 'featured-grid');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_featured_content_block_columns" AS ENUM('2', '3', '4');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_featured_content_block_background_color" AS ENUM('white', 'ivory', 'sand', 'light', 'dark');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_featured_content_block_card_bg" AS ENUM('white', 'transparent', 'light');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

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

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_featured_content_block_card_hover" AS ENUM('none', 'lift', 'zoom', 'shadow');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE "public"."enum_pages_blocks_featured_content_block_items_item_type" AS ENUM('post', 'video', 'series', 'external');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

-- =====================================================
-- FAVORITES BLOCK TABLES
-- =====================================================

CREATE TABLE IF NOT EXISTS "pages_blocks_favorites_block" (
  "_order" integer NOT NULL,
  "_parent_id" integer NOT NULL,
  "_path" text NOT NULL,
  "id" varchar PRIMARY KEY NOT NULL,
  "category" "enum_pages_blocks_favorites_block_category" DEFAULT 'all',
  "show_featured_only" boolean DEFAULT false,
  "limit" numeric DEFAULT 8,
  "layout" "enum_pages_blocks_favorites_block_layout" DEFAULT 'grid',
  "columns" "enum_pages_blocks_favorites_block_columns" DEFAULT '4',
  "show_price" boolean DEFAULT true,
  "show_badge" boolean DEFAULT true,
  "show_description" boolean DEFAULT false,
  "show_category" boolean DEFAULT false,
  "background_color" "enum_pages_blocks_favorites_block_background_color" DEFAULT 'white',
  "cta_show_cta" boolean DEFAULT false,
  "cta_cta_url" varchar,
  "block_name" varchar
);

CREATE TABLE IF NOT EXISTS "pages_blocks_favorites_block_locales" (
  "title" varchar,
  "subtitle" varchar,
  "cta_cta_text" varchar DEFAULT 'Alle Favoriten ansehen',
  "id" serial PRIMARY KEY NOT NULL,
  "_locale" "_locales" NOT NULL,
  "_parent_id" varchar NOT NULL,
  CONSTRAINT "pages_blocks_favorites_block_locales_locale_parent_id_unique" UNIQUE("_locale","_parent_id")
);

-- =====================================================
-- SERIES BLOCK TABLES
-- =====================================================

CREATE TABLE IF NOT EXISTS "pages_blocks_series_block" (
  "_order" integer NOT NULL,
  "_parent_id" integer NOT NULL,
  "_path" text NOT NULL,
  "id" varchar PRIMARY KEY NOT NULL,
  "layout" "enum_pages_blocks_series_block_layout" DEFAULT 'grid',
  "columns" "enum_pages_blocks_series_block_columns" DEFAULT '3',
  "show_description" boolean DEFAULT true,
  "show_logo" boolean DEFAULT true,
  "show_tagline" boolean DEFAULT true,
  "use_brand_colors" boolean DEFAULT true,
  "limit" numeric DEFAULT 6,
  "background_color" "enum_pages_blocks_series_block_background_color" DEFAULT 'white',
  "cta_show_cta" boolean DEFAULT false,
  "cta_cta_url" varchar,
  "block_name" varchar
);

CREATE TABLE IF NOT EXISTS "pages_blocks_series_block_locales" (
  "title" varchar,
  "subtitle" varchar,
  "cta_cta_text" varchar DEFAULT 'Alle Serien ansehen',
  "id" serial PRIMARY KEY NOT NULL,
  "_locale" "_locales" NOT NULL,
  "_parent_id" varchar NOT NULL,
  CONSTRAINT "pages_blocks_series_block_locales_locale_parent_id_unique" UNIQUE("_locale","_parent_id")
);

-- =====================================================
-- SERIES DETAIL BLOCK TABLES
-- =====================================================

CREATE TABLE IF NOT EXISTS "pages_blocks_series_detail_block" (
  "_order" integer NOT NULL,
  "_parent_id" integer NOT NULL,
  "_path" text NOT NULL,
  "id" varchar PRIMARY KEY NOT NULL,
  "series_id" integer,
  "show_hero" boolean DEFAULT true,
  "show_description" boolean DEFAULT true,
  "show_brand_colors" boolean DEFAULT true,
  "show_related_posts" boolean DEFAULT true,
  "related_posts_limit" numeric DEFAULT 6,
  "show_youtube_playlist" boolean DEFAULT true,
  "layout" "enum_pages_blocks_series_detail_block_layout" DEFAULT 'full',
  "hero_height" "enum_pages_blocks_series_detail_block_hero_height" DEFAULT 'medium',
  "hero_overlay" boolean DEFAULT true,
  "hero_text_align" "enum_pages_blocks_series_detail_block_hero_text_align" DEFAULT 'center',
  "block_name" varchar
);

CREATE TABLE IF NOT EXISTS "pages_blocks_series_detail_block_locales" (
  "related_posts_title" varchar DEFAULT 'Beiträge aus dieser Serie',
  "id" serial PRIMARY KEY NOT NULL,
  "_locale" "_locales" NOT NULL,
  "_parent_id" varchar NOT NULL,
  CONSTRAINT "pages_blocks_series_detail_block_locales_locale_parent_id_unique" UNIQUE("_locale","_parent_id")
);

-- Relationship table for series field
CREATE TABLE IF NOT EXISTS "pages_blocks_series_detail_block_rels" (
  "id" serial PRIMARY KEY NOT NULL,
  "order" integer,
  "parent_id" varchar NOT NULL,
  "path" varchar NOT NULL,
  "series_id" integer
);

-- =====================================================
-- FEATURED CONTENT BLOCK TABLES
-- =====================================================

CREATE TABLE IF NOT EXISTS "pages_blocks_featured_content_block" (
  "_order" integer NOT NULL,
  "_parent_id" integer NOT NULL,
  "_path" text NOT NULL,
  "id" varchar PRIMARY KEY NOT NULL,
  "layout" "enum_pages_blocks_featured_content_block_layout" DEFAULT 'grid',
  "columns" "enum_pages_blocks_featured_content_block_columns" DEFAULT '3',
  "show_dates" boolean DEFAULT true,
  "show_type" boolean DEFAULT true,
  "show_description" boolean DEFAULT true,
  "show_custom_labels" boolean DEFAULT true,
  "background_color" "enum_pages_blocks_featured_content_block_background_color" DEFAULT 'white',
  "card_bg" "enum_pages_blocks_featured_content_block_card_bg" DEFAULT 'white',
  "card_shadow" boolean DEFAULT true,
  "card_border" boolean DEFAULT false,
  "card_img_ratio" "enum_pages_blocks_featured_content_block_card_img_ratio" DEFAULT '16:9',
  "card_hover" "enum_pages_blocks_featured_content_block_card_hover" DEFAULT 'lift',
  "cta_show_cta" boolean DEFAULT false,
  "cta_cta_url" varchar,
  "block_name" varchar
);

CREATE TABLE IF NOT EXISTS "pages_blocks_featured_content_block_locales" (
  "title" varchar,
  "subtitle" varchar,
  "cta_cta_text" varchar DEFAULT 'Alle ansehen',
  "id" serial PRIMARY KEY NOT NULL,
  "_locale" "_locales" NOT NULL,
  "_parent_id" varchar NOT NULL,
  CONSTRAINT "pages_blocks_featured_content_block_locales_locale_parent_id_unique" UNIQUE("_locale","_parent_id")
);

-- Items array table for featured content
CREATE TABLE IF NOT EXISTS "pages_blocks_featured_content_block_items" (
  "_order" integer NOT NULL,
  "_parent_id" varchar NOT NULL,
  "id" varchar PRIMARY KEY NOT NULL,
  "item_type" "enum_pages_blocks_featured_content_block_items_item_type",
  "post_id" integer,
  "video_id" integer,
  "series_id" integer,
  "external_url" varchar,
  "external_image_id" integer,
  "featured" boolean DEFAULT false
);

CREATE TABLE IF NOT EXISTS "pages_blocks_featured_content_block_items_locales" (
  "external_title" varchar,
  "external_description" varchar,
  "custom_label" varchar,
  "id" serial PRIMARY KEY NOT NULL,
  "_locale" "_locales" NOT NULL,
  "_parent_id" varchar NOT NULL,
  CONSTRAINT "pages_blocks_featured_content_block_items_locales_locale_parent_id_unique" UNIQUE("_locale","_parent_id")
);

-- Relationship table for items (post, video, series references)
CREATE TABLE IF NOT EXISTS "pages_blocks_featured_content_block_items_rels" (
  "id" serial PRIMARY KEY NOT NULL,
  "order" integer,
  "parent_id" varchar NOT NULL,
  "path" varchar NOT NULL,
  "posts_id" integer,
  "videos_id" integer,
  "series_id" integer
);

-- =====================================================
-- INDEXES
-- =====================================================

-- Favorites Block indexes
CREATE INDEX IF NOT EXISTS "pages_blocks_favorites_block_order_idx" ON "pages_blocks_favorites_block" ("_order");
CREATE INDEX IF NOT EXISTS "pages_blocks_favorites_block_parent_id_idx" ON "pages_blocks_favorites_block" ("_parent_id");
CREATE INDEX IF NOT EXISTS "pages_blocks_favorites_block_path_idx" ON "pages_blocks_favorites_block" ("_path");

-- Series Block indexes
CREATE INDEX IF NOT EXISTS "pages_blocks_series_block_order_idx" ON "pages_blocks_series_block" ("_order");
CREATE INDEX IF NOT EXISTS "pages_blocks_series_block_parent_id_idx" ON "pages_blocks_series_block" ("_parent_id");
CREATE INDEX IF NOT EXISTS "pages_blocks_series_block_path_idx" ON "pages_blocks_series_block" ("_path");

-- Series Detail Block indexes
CREATE INDEX IF NOT EXISTS "pages_blocks_series_detail_block_order_idx" ON "pages_blocks_series_detail_block" ("_order");
CREATE INDEX IF NOT EXISTS "pages_blocks_series_detail_block_parent_id_idx" ON "pages_blocks_series_detail_block" ("_parent_id");
CREATE INDEX IF NOT EXISTS "pages_blocks_series_detail_block_path_idx" ON "pages_blocks_series_detail_block" ("_path");
CREATE INDEX IF NOT EXISTS "pages_blocks_series_detail_block_series_idx" ON "pages_blocks_series_detail_block" ("series_id");
CREATE INDEX IF NOT EXISTS "pages_blocks_series_detail_block_rels_order_idx" ON "pages_blocks_series_detail_block_rels" ("order");
CREATE INDEX IF NOT EXISTS "pages_blocks_series_detail_block_rels_parent_fk" ON "pages_blocks_series_detail_block_rels" ("parent_id");
CREATE INDEX IF NOT EXISTS "pages_blocks_series_detail_block_rels_path_idx" ON "pages_blocks_series_detail_block_rels" ("path");
CREATE INDEX IF NOT EXISTS "pages_blocks_series_detail_block_rels_series_idx" ON "pages_blocks_series_detail_block_rels" ("series_id");

-- Featured Content Block indexes
CREATE INDEX IF NOT EXISTS "pages_blocks_featured_content_block_order_idx" ON "pages_blocks_featured_content_block" ("_order");
CREATE INDEX IF NOT EXISTS "pages_blocks_featured_content_block_parent_id_idx" ON "pages_blocks_featured_content_block" ("_parent_id");
CREATE INDEX IF NOT EXISTS "pages_blocks_featured_content_block_path_idx" ON "pages_blocks_featured_content_block" ("_path");

CREATE INDEX IF NOT EXISTS "pages_blocks_featured_content_block_items_order_idx" ON "pages_blocks_featured_content_block_items" ("_order");
CREATE INDEX IF NOT EXISTS "pages_blocks_featured_content_block_items_parent_fk" ON "pages_blocks_featured_content_block_items" ("_parent_id");
CREATE INDEX IF NOT EXISTS "pages_blocks_featured_content_block_items_post_idx" ON "pages_blocks_featured_content_block_items" ("post_id");
CREATE INDEX IF NOT EXISTS "pages_blocks_featured_content_block_items_video_idx" ON "pages_blocks_featured_content_block_items" ("video_id");
CREATE INDEX IF NOT EXISTS "pages_blocks_featured_content_block_items_series_idx" ON "pages_blocks_featured_content_block_items" ("series_id");

CREATE INDEX IF NOT EXISTS "pages_blocks_featured_content_block_items_rels_order_idx" ON "pages_blocks_featured_content_block_items_rels" ("order");
CREATE INDEX IF NOT EXISTS "pages_blocks_featured_content_block_items_rels_parent_fk" ON "pages_blocks_featured_content_block_items_rels" ("parent_id");
CREATE INDEX IF NOT EXISTS "pages_blocks_featured_content_block_items_rels_path_idx" ON "pages_blocks_featured_content_block_items_rels" ("path");

-- =====================================================
-- FOREIGN KEY CONSTRAINTS
-- =====================================================

DO $$ BEGIN
  ALTER TABLE "pages_blocks_favorites_block" ADD CONSTRAINT "pages_blocks_favorites_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_favorites_block_locales" ADD CONSTRAINT "pages_blocks_favorites_block_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_favorites_block"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_series_block" ADD CONSTRAINT "pages_blocks_series_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_series_block_locales" ADD CONSTRAINT "pages_blocks_series_block_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_series_block"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_series_detail_block" ADD CONSTRAINT "pages_blocks_series_detail_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_series_detail_block_locales" ADD CONSTRAINT "pages_blocks_series_detail_block_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_series_detail_block"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_series_detail_block_rels" ADD CONSTRAINT "pages_blocks_series_detail_block_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."pages_blocks_series_detail_block"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_series_detail_block_rels" ADD CONSTRAINT "pages_blocks_series_detail_block_rels_series_fk" FOREIGN KEY ("series_id") REFERENCES "public"."series"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_series_detail_block" ADD CONSTRAINT "pages_blocks_series_detail_block_series_fk" FOREIGN KEY ("series_id") REFERENCES "public"."series"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_featured_content_block" ADD CONSTRAINT "pages_blocks_featured_content_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_featured_content_block_locales" ADD CONSTRAINT "pages_blocks_featured_content_block_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_featured_content_block"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

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

DO $$ BEGIN
  ALTER TABLE "pages_blocks_featured_content_block_items" ADD CONSTRAINT "pages_blocks_featured_content_block_items_external_image_fk" FOREIGN KEY ("external_image_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_featured_content_block_items" ADD CONSTRAINT "pages_blocks_featured_content_block_items_post_fk" FOREIGN KEY ("post_id") REFERENCES "public"."posts"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_featured_content_block_items" ADD CONSTRAINT "pages_blocks_featured_content_block_items_video_fk" FOREIGN KEY ("video_id") REFERENCES "public"."videos"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_featured_content_block_items" ADD CONSTRAINT "pages_blocks_featured_content_block_items_series_fk" FOREIGN KEY ("series_id") REFERENCES "public"."series"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

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

DO $$ BEGIN
  ALTER TABLE "pages_blocks_featured_content_block_items_rels" ADD CONSTRAINT "pages_blocks_featured_content_block_items_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."pages_blocks_featured_content_block_items"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_featured_content_block_items_rels" ADD CONSTRAINT "pages_blocks_featured_content_block_items_rels_posts_fk" FOREIGN KEY ("posts_id") REFERENCES "public"."posts"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_featured_content_block_items_rels" ADD CONSTRAINT "pages_blocks_featured_content_block_items_rels_videos_fk" FOREIGN KEY ("videos_id") REFERENCES "public"."videos"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_blocks_featured_content_block_items_rels" ADD CONSTRAINT "pages_blocks_featured_content_block_items_rels_series_fk" FOREIGN KEY ("series_id") REFERENCES "public"."series"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

  `);
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
-- Drop tables in reverse order (child tables first)
DROP TABLE IF EXISTS "pages_blocks_featured_content_block_items_rels";
DROP TABLE IF EXISTS "pages_blocks_featured_content_block_items_locales";
DROP TABLE IF EXISTS "pages_blocks_featured_content_block_items";
DROP TABLE IF EXISTS "pages_blocks_featured_content_block_locales";
DROP TABLE IF EXISTS "pages_blocks_featured_content_block";

DROP TABLE IF EXISTS "pages_blocks_series_detail_block_rels";
DROP TABLE IF EXISTS "pages_blocks_series_detail_block_locales";
DROP TABLE IF EXISTS "pages_blocks_series_detail_block";

DROP TABLE IF EXISTS "pages_blocks_series_block_locales";
DROP TABLE IF EXISTS "pages_blocks_series_block";

DROP TABLE IF EXISTS "pages_blocks_favorites_block_locales";
DROP TABLE IF EXISTS "pages_blocks_favorites_block";

-- Drop ENUMs
DROP TYPE IF EXISTS "enum_pages_blocks_featured_content_block_items_item_type";
DROP TYPE IF EXISTS "enum_pages_blocks_featured_content_block_card_hover";
DROP TYPE IF EXISTS "enum_pages_blocks_featured_content_block_card_img_ratio";
DROP TYPE IF EXISTS "enum_pages_blocks_featured_content_block_card_bg";
DROP TYPE IF EXISTS "enum_pages_blocks_featured_content_block_background_color";
DROP TYPE IF EXISTS "enum_pages_blocks_featured_content_block_columns";
DROP TYPE IF EXISTS "enum_pages_blocks_featured_content_block_layout";
DROP TYPE IF EXISTS "enum_pages_blocks_series_detail_block_hero_text_align";
DROP TYPE IF EXISTS "enum_pages_blocks_series_detail_block_hero_height";
DROP TYPE IF EXISTS "enum_pages_blocks_series_detail_block_layout";
DROP TYPE IF EXISTS "enum_pages_blocks_series_block_background_color";
DROP TYPE IF EXISTS "enum_pages_blocks_series_block_columns";
DROP TYPE IF EXISTS "enum_pages_blocks_series_block_layout";
DROP TYPE IF EXISTS "enum_pages_blocks_favorites_block_background_color";
DROP TYPE IF EXISTS "enum_pages_blocks_favorites_block_columns";
DROP TYPE IF EXISTS "enum_pages_blocks_favorites_block_layout";
DROP TYPE IF EXISTS "enum_pages_blocks_favorites_block_category";
  `);
}
