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

/**
 * Migration: ImageSliderBlock
 *
 * Erstellt den ImageSliderBlock für Bildergalerien mit:
 * - Verschiedenen Layouts (Slider, Grid, Masonry)
 * - Lightbox-Funktion
 * - Flexible Slider-Optionen
 * - Styling und Navigation
 */

export async function up({ db }: MigrateUpArgs): Promise<void> {
  // Create enums
  await db.execute(sql`
    -- Layout
    CREATE TYPE "enum_pages_blocks_image_slider_block_layout" AS ENUM('slider', 'grid', 'masonry', 'single', 'thumbs', 'filmstrip');

    -- Grid
    CREATE TYPE "enum_pages_blocks_image_slider_block_grid_cols" AS ENUM('2', '3', '4', '5', '6');
    CREATE TYPE "enum_pages_blocks_image_slider_block_grid_gap" AS ENUM('0', '8', '16', '24', '32');
    CREATE TYPE "enum_pages_blocks_image_slider_block_grid_aspect" AS ENUM('square', '4-3', '16-9', '3-4', 'auto');

    -- Slider
    CREATE TYPE "enum_pages_blocks_image_slider_block_slider_effect" AS ENUM('slide', 'fade', 'zoom', 'flip', 'coverflow', 'cube');
    CREATE TYPE "enum_pages_blocks_image_slider_block_slider_speed" AS ENUM('300', '500', '800');
    CREATE TYPE "enum_pages_blocks_image_slider_block_slider_per_view" AS ENUM('1', '2', '3', '4', 'auto');
    CREATE TYPE "enum_pages_blocks_image_slider_block_slider_gap" AS ENUM('0', '8', '16', '24');
    CREATE TYPE "enum_pages_blocks_image_slider_block_slider_delay" AS ENUM('3000', '4000', '5000', '6000', '8000');

    -- Navigation
    CREATE TYPE "enum_pages_blocks_image_slider_block_nav_arrow_style" AS ENUM('default', 'minimal', 'square', 'hover');
    CREATE TYPE "enum_pages_blocks_image_slider_block_nav_dot_style" AS ENUM('dots', 'lines', 'numbers', 'progress');

    -- Lightbox
    CREATE TYPE "enum_pages_blocks_image_slider_block_lightbox_bg" AS ENUM('dark', 'light', 'blur');

    -- Style
    CREATE TYPE "enum_pages_blocks_image_slider_block_style_height" AS ENUM('auto', '300', '400', '500', '600', 'full');
    CREATE TYPE "enum_pages_blocks_image_slider_block_style_img_fit" AS ENUM('cover', 'contain', 'fill');
    CREATE TYPE "enum_pages_blocks_image_slider_block_style_rounded" AS ENUM('none', 'sm', 'md', 'lg', 'full');
    CREATE TYPE "enum_pages_blocks_image_slider_block_style_hover" AS ENUM('none', 'zoom', 'brighten', 'darken', 'overlay');
    CREATE TYPE "enum_pages_blocks_image_slider_block_style_caption" AS ENUM('below', 'overlay', 'hover', 'hidden');
    CREATE TYPE "enum_pages_blocks_image_slider_block_style_bg" AS ENUM('none', 'white', 'light', 'dark');
    CREATE TYPE "enum_pages_blocks_image_slider_block_style_padding" AS ENUM('none', 'sm', 'md', 'lg');
  `)

  // Create images array table
  await db.execute(sql`
    CREATE TABLE "pages_blocks_image_slider_block_images" (
      "_order" integer NOT NULL,
      "_parent_id" varchar NOT NULL,
      "id" varchar PRIMARY KEY NOT NULL,
      "image_id" integer NOT NULL,
      "link" varchar
    );
  `)

  // Create images locales table
  await db.execute(sql`
    CREATE TABLE "pages_blocks_image_slider_block_images_locales" (
      "caption" varchar,
      "alt" varchar,
      "id" serial PRIMARY KEY NOT NULL,
      "_locale" "_locales" NOT NULL,
      "_parent_id" varchar NOT NULL
    );
  `)

  // Create main block table
  await db.execute(sql`
    CREATE TABLE "pages_blocks_image_slider_block" (
      "_order" integer NOT NULL,
      "_parent_id" integer NOT NULL,
      "_path" text NOT NULL,
      "id" varchar PRIMARY KEY NOT NULL,

      -- Layout
      "layout" "enum_pages_blocks_image_slider_block_layout" DEFAULT 'slider',

      -- Grid settings
      "grid_cols" "enum_pages_blocks_image_slider_block_grid_cols" DEFAULT '3',
      "grid_gap" "enum_pages_blocks_image_slider_block_grid_gap" DEFAULT '16',
      "grid_aspect_ratio" "enum_pages_blocks_image_slider_block_grid_aspect" DEFAULT 'square',

      -- Slider settings
      "slider_effect" "enum_pages_blocks_image_slider_block_slider_effect" DEFAULT 'slide',
      "slider_speed" "enum_pages_blocks_image_slider_block_slider_speed" DEFAULT '500',
      "slider_per_view" "enum_pages_blocks_image_slider_block_slider_per_view" DEFAULT '1',
      "slider_gap" "enum_pages_blocks_image_slider_block_slider_gap" DEFAULT '0',
      "slider_auto" boolean DEFAULT false,
      "slider_delay" "enum_pages_blocks_image_slider_block_slider_delay" DEFAULT '4000',
      "slider_pause_hover" boolean DEFAULT true,
      "slider_loop" boolean DEFAULT true,
      "slider_centered" boolean DEFAULT false,

      -- Navigation
      "nav_arrows" boolean DEFAULT true,
      "nav_arrow_style" "enum_pages_blocks_image_slider_block_nav_arrow_style" DEFAULT 'default',
      "nav_dots" boolean DEFAULT true,
      "nav_dot_style" "enum_pages_blocks_image_slider_block_nav_dot_style" DEFAULT 'dots',
      "nav_counter" boolean DEFAULT false,
      "nav_swipe" boolean DEFAULT true,
      "nav_keys" boolean DEFAULT true,

      -- Lightbox
      "lightbox_enabled" boolean DEFAULT true,
      "lightbox_zoom" boolean DEFAULT true,
      "lightbox_download" boolean DEFAULT false,
      "lightbox_share" boolean DEFAULT false,
      "lightbox_captions" boolean DEFAULT true,
      "lightbox_thumbs" boolean DEFAULT true,
      "lightbox_bg" "enum_pages_blocks_image_slider_block_lightbox_bg" DEFAULT 'dark',

      -- Style
      "style_height" "enum_pages_blocks_image_slider_block_style_height" DEFAULT 'auto',
      "style_img_fit" "enum_pages_blocks_image_slider_block_style_img_fit" DEFAULT 'cover',
      "style_rounded" "enum_pages_blocks_image_slider_block_style_rounded" DEFAULT 'none',
      "style_shadow" boolean DEFAULT false,
      "style_border" boolean DEFAULT false,
      "style_hover_effect" "enum_pages_blocks_image_slider_block_style_hover" DEFAULT 'none',
      "style_caption_pos" "enum_pages_blocks_image_slider_block_style_caption" DEFAULT 'below',
      "style_bg" "enum_pages_blocks_image_slider_block_style_bg" DEFAULT 'none',
      "style_padding" "enum_pages_blocks_image_slider_block_style_padding" DEFAULT 'none',

      -- Accessibility
      "a11y_reduced_motion" boolean DEFAULT true,

      "block_name" varchar
    );
  `)

  // Create locales table
  await db.execute(sql`
    CREATE TABLE "pages_blocks_image_slider_block_locales" (
      "title" varchar,
      "description" varchar,
      "a11y_label" varchar DEFAULT 'Bildergalerie',
      "id" serial PRIMARY KEY NOT NULL,
      "_locale" "_locales" NOT NULL,
      "_parent_id" varchar NOT NULL
    );
  `)

  // Add foreign keys and indexes
  await db.execute(sql`
    -- Images table
    ALTER TABLE "pages_blocks_image_slider_block_images"
      ADD CONSTRAINT "pages_blocks_image_slider_block_images_image_id_fk"
      FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;

    ALTER TABLE "pages_blocks_image_slider_block_images"
      ADD CONSTRAINT "pages_blocks_image_slider_block_images_parent_id_fk"
      FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_image_slider_block"("id") ON DELETE cascade ON UPDATE no action;

    CREATE INDEX "pages_blocks_image_slider_block_images_order_idx"
      ON "pages_blocks_image_slider_block_images" USING btree ("_order");
    CREATE INDEX "pages_blocks_image_slider_block_images_parent_id_idx"
      ON "pages_blocks_image_slider_block_images" USING btree ("_parent_id");
    CREATE INDEX "pages_blocks_image_slider_block_images_image_idx"
      ON "pages_blocks_image_slider_block_images" USING btree ("image_id");

    -- Images locales table
    ALTER TABLE "pages_blocks_image_slider_block_images_locales"
      ADD CONSTRAINT "pages_blocks_image_slider_block_images_locales_parent_fk"
      FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_image_slider_block_images"("id") ON DELETE cascade ON UPDATE no action;

    CREATE UNIQUE INDEX "pages_blocks_image_slider_block_images_locales_uniq"
      ON "pages_blocks_image_slider_block_images_locales" USING btree ("_locale", "_parent_id");

    -- Main block table
    ALTER TABLE "pages_blocks_image_slider_block"
      ADD CONSTRAINT "pages_blocks_image_slider_block_parent_id_fk"
      FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;

    CREATE INDEX "pages_blocks_image_slider_block_order_idx"
      ON "pages_blocks_image_slider_block" USING btree ("_order");
    CREATE INDEX "pages_blocks_image_slider_block_parent_id_idx"
      ON "pages_blocks_image_slider_block" USING btree ("_parent_id");
    CREATE INDEX "pages_blocks_image_slider_block_path_idx"
      ON "pages_blocks_image_slider_block" USING btree ("_path");

    -- Locales table
    ALTER TABLE "pages_blocks_image_slider_block_locales"
      ADD CONSTRAINT "pages_blocks_image_slider_block_locales_parent_id_fk"
      FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_image_slider_block"("id") ON DELETE cascade ON UPDATE no action;

    CREATE UNIQUE INDEX "pages_blocks_image_slider_block_locales_locale_parent_uniq"
      ON "pages_blocks_image_slider_block_locales" USING btree ("_locale", "_parent_id");
  `)
}

export async function down({ db }: MigrateDownArgs): Promise<void> {
  // Drop tables
  await db.execute(sql`
    DROP TABLE IF EXISTS "pages_blocks_image_slider_block_images_locales" CASCADE;
    DROP TABLE IF EXISTS "pages_blocks_image_slider_block_images" CASCADE;
    DROP TABLE IF EXISTS "pages_blocks_image_slider_block_locales" CASCADE;
    DROP TABLE IF EXISTS "pages_blocks_image_slider_block" CASCADE;
  `)

  // Drop enums
  await db.execute(sql`
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_layout";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_grid_cols";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_grid_gap";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_grid_aspect";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_slider_effect";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_slider_speed";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_slider_per_view";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_slider_gap";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_slider_delay";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_nav_arrow_style";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_nav_dot_style";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_lightbox_bg";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_style_height";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_style_img_fit";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_style_rounded";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_style_hover";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_style_caption";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_style_bg";
    DROP TYPE IF EXISTS "enum_pages_blocks_image_slider_block_style_padding";
  `)
}
