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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
    -- Posts List Block Table
    CREATE TABLE IF NOT EXISTS "pages_blocks_posts_list_block" (
      "id" serial PRIMARY KEY NOT NULL,
      "_parent_id" integer NOT NULL,
      "_order" integer NOT NULL,
      "_path" varchar NOT NULL,
      "title" varchar,
      "subtitle" varchar,
      "post_type" varchar DEFAULT 'blog',
      "layout" varchar DEFAULT 'grid',
      "columns" varchar DEFAULT '3',
      "limit" integer DEFAULT 6,
      "show_featured_only" boolean DEFAULT false,
      "show_excerpt" boolean DEFAULT true,
      "show_date" boolean DEFAULT true,
      "show_author" boolean DEFAULT false,
      "show_category" boolean DEFAULT true,
      "show_pagination" boolean DEFAULT false,
      "show_read_more" boolean DEFAULT true,
      "read_more_label" varchar DEFAULT 'Alle Beiträge anzeigen',
      "read_more_link" varchar DEFAULT '/blog',
      "background_color" varchar DEFAULT 'white',
      "block_name" varchar
    );

    CREATE INDEX IF NOT EXISTS "pages_blocks_posts_list_block_parent_idx" ON "pages_blocks_posts_list_block" USING btree ("_parent_id");
    CREATE INDEX IF NOT EXISTS "pages_blocks_posts_list_block_order_idx" ON "pages_blocks_posts_list_block" USING btree ("_order");
    CREATE INDEX IF NOT EXISTS "pages_blocks_posts_list_block_path_idx" ON "pages_blocks_posts_list_block" USING btree ("_path");

    ALTER TABLE "pages_blocks_posts_list_block" DROP CONSTRAINT IF EXISTS "pages_blocks_posts_list_block_parent_id_fk";
    ALTER TABLE "pages_blocks_posts_list_block" ADD CONSTRAINT "pages_blocks_posts_list_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;

    -- Testimonials Block Table
    CREATE TABLE IF NOT EXISTS "pages_blocks_testimonials_block" (
      "id" serial PRIMARY KEY NOT NULL,
      "_parent_id" integer NOT NULL,
      "_order" integer NOT NULL,
      "_path" varchar NOT NULL,
      "title" varchar DEFAULT 'Das sagen unsere Kunden',
      "subtitle" varchar,
      "layout" varchar DEFAULT 'slider',
      "columns" varchar DEFAULT '3',
      "display_mode" varchar DEFAULT 'all',
      "limit" integer DEFAULT 6,
      "show_rating" boolean DEFAULT true,
      "show_image" boolean DEFAULT true,
      "show_company" boolean DEFAULT true,
      "show_source" boolean DEFAULT false,
      "autoplay" boolean DEFAULT true,
      "autoplay_speed" integer DEFAULT 5000,
      "background_color" varchar DEFAULT 'light',
      "block_name" varchar
    );

    CREATE INDEX IF NOT EXISTS "pages_blocks_testimonials_block_parent_idx" ON "pages_blocks_testimonials_block" USING btree ("_parent_id");
    CREATE INDEX IF NOT EXISTS "pages_blocks_testimonials_block_order_idx" ON "pages_blocks_testimonials_block" USING btree ("_order");
    CREATE INDEX IF NOT EXISTS "pages_blocks_testimonials_block_path_idx" ON "pages_blocks_testimonials_block" USING btree ("_path");

    ALTER TABLE "pages_blocks_testimonials_block" DROP CONSTRAINT IF EXISTS "pages_blocks_testimonials_block_parent_id_fk";
    ALTER TABLE "pages_blocks_testimonials_block" ADD CONSTRAINT "pages_blocks_testimonials_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;

    -- Newsletter Block Table
    CREATE TABLE IF NOT EXISTS "pages_blocks_newsletter_block" (
      "id" serial PRIMARY KEY NOT NULL,
      "_parent_id" integer NOT NULL,
      "_order" integer NOT NULL,
      "_path" varchar NOT NULL,
      "title" varchar DEFAULT 'Newsletter abonnieren',
      "subtitle" varchar,
      "layout" varchar DEFAULT 'inline',
      "image_id" integer,
      "image_position" varchar DEFAULT 'left',
      "collect_name" boolean DEFAULT false,
      "show_interests" boolean DEFAULT false,
      "button_text" varchar DEFAULT 'Anmelden',
      "placeholder_email" varchar DEFAULT 'Ihre E-Mail-Adresse',
      "success_message" varchar,
      "error_message" varchar,
      "privacy_text" varchar,
      "privacy_link" varchar DEFAULT '/datenschutz',
      "source" varchar DEFAULT 'website',
      "background_color" varchar DEFAULT 'accent',
      "block_name" varchar
    );

    CREATE INDEX IF NOT EXISTS "pages_blocks_newsletter_block_parent_idx" ON "pages_blocks_newsletter_block" USING btree ("_parent_id");
    CREATE INDEX IF NOT EXISTS "pages_blocks_newsletter_block_order_idx" ON "pages_blocks_newsletter_block" USING btree ("_order");
    CREATE INDEX IF NOT EXISTS "pages_blocks_newsletter_block_path_idx" ON "pages_blocks_newsletter_block" USING btree ("_path");

    ALTER TABLE "pages_blocks_newsletter_block" DROP CONSTRAINT IF EXISTS "pages_blocks_newsletter_block_parent_id_fk";
    ALTER TABLE "pages_blocks_newsletter_block" ADD CONSTRAINT "pages_blocks_newsletter_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
    ALTER TABLE "pages_blocks_newsletter_block" DROP CONSTRAINT IF EXISTS "pages_blocks_newsletter_block_image_id_fk";
    ALTER TABLE "pages_blocks_newsletter_block" ADD CONSTRAINT "pages_blocks_newsletter_block_image_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;

    -- Newsletter Block Available Interests (hasMany select)
    CREATE TABLE IF NOT EXISTS "pages_blocks_newsletter_block_available_interests" (
      "id" serial PRIMARY KEY NOT NULL,
      "parent_id" integer NOT NULL,
      "order" integer NOT NULL,
      "value" varchar
    );

    CREATE INDEX IF NOT EXISTS "pages_blocks_newsletter_block_available_interests_parent_idx" ON "pages_blocks_newsletter_block_available_interests" USING btree ("parent_id");
    CREATE INDEX IF NOT EXISTS "pages_blocks_newsletter_block_available_interests_order_idx" ON "pages_blocks_newsletter_block_available_interests" USING btree ("order");

    ALTER TABLE "pages_blocks_newsletter_block_available_interests" DROP CONSTRAINT IF EXISTS "pages_blocks_newsletter_block_available_interests_parent_id_fk";
    ALTER TABLE "pages_blocks_newsletter_block_available_interests" ADD CONSTRAINT "pages_blocks_newsletter_block_available_interests_parent_id_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."pages_blocks_newsletter_block"("id") ON DELETE cascade ON UPDATE no action;

    -- Process Steps Block Table
    CREATE TABLE IF NOT EXISTS "pages_blocks_process_steps_block" (
      "id" serial PRIMARY KEY NOT NULL,
      "_parent_id" integer NOT NULL,
      "_order" integer NOT NULL,
      "_path" varchar NOT NULL,
      "title" varchar DEFAULT 'So funktioniert es',
      "subtitle" varchar,
      "layout" varchar DEFAULT 'horizontal',
      "show_numbers" boolean DEFAULT true,
      "show_icons" boolean DEFAULT true,
      "cta_show" boolean DEFAULT false,
      "cta_label" varchar DEFAULT 'Jetzt starten',
      "cta_href" varchar,
      "cta_variant" varchar DEFAULT 'default',
      "background_color" varchar DEFAULT 'white',
      "block_name" varchar
    );

    CREATE INDEX IF NOT EXISTS "pages_blocks_process_steps_block_parent_idx" ON "pages_blocks_process_steps_block" USING btree ("_parent_id");
    CREATE INDEX IF NOT EXISTS "pages_blocks_process_steps_block_order_idx" ON "pages_blocks_process_steps_block" USING btree ("_order");
    CREATE INDEX IF NOT EXISTS "pages_blocks_process_steps_block_path_idx" ON "pages_blocks_process_steps_block" USING btree ("_path");

    ALTER TABLE "pages_blocks_process_steps_block" DROP CONSTRAINT IF EXISTS "pages_blocks_process_steps_block_parent_id_fk";
    ALTER TABLE "pages_blocks_process_steps_block" ADD CONSTRAINT "pages_blocks_process_steps_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;

    -- Process Steps Block Steps (array)
    CREATE TABLE IF NOT EXISTS "pages_blocks_process_steps_block_steps" (
      "id" serial PRIMARY KEY NOT NULL,
      "_parent_id" integer NOT NULL,
      "_order" integer NOT NULL,
      "title" varchar NOT NULL,
      "description" varchar,
      "icon" varchar,
      "image_id" integer
    );

    CREATE INDEX IF NOT EXISTS "pages_blocks_process_steps_block_steps_parent_idx" ON "pages_blocks_process_steps_block_steps" USING btree ("_parent_id");
    CREATE INDEX IF NOT EXISTS "pages_blocks_process_steps_block_steps_order_idx" ON "pages_blocks_process_steps_block_steps" USING btree ("_order");

    ALTER TABLE "pages_blocks_process_steps_block_steps" DROP CONSTRAINT IF EXISTS "pages_blocks_process_steps_block_steps_parent_id_fk";
    ALTER TABLE "pages_blocks_process_steps_block_steps" ADD CONSTRAINT "pages_blocks_process_steps_block_steps_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_process_steps_block"("id") ON DELETE cascade ON UPDATE no action;
    ALTER TABLE "pages_blocks_process_steps_block_steps" DROP CONSTRAINT IF EXISTS "pages_blocks_process_steps_block_steps_image_id_fk";
    ALTER TABLE "pages_blocks_process_steps_block_steps" ADD CONSTRAINT "pages_blocks_process_steps_block_steps_image_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;

    -- SEO Settings Global
    CREATE TABLE IF NOT EXISTS "seo_settings" (
      "id" serial PRIMARY KEY NOT NULL,
      "meta_defaults_title_suffix" varchar DEFAULT '| Website',
      "meta_defaults_default_description" varchar,
      "meta_defaults_default_og_image_id" integer,
      "organization_name" varchar,
      "organization_legal_name" varchar,
      "organization_description" varchar,
      "organization_logo_id" integer,
      "organization_founding_date" timestamp(3) with time zone,
      "contact_email" varchar,
      "contact_phone" varchar,
      "contact_fax" varchar,
      "address_street" varchar,
      "address_postal_code" varchar,
      "address_city" varchar,
      "address_region" varchar,
      "address_country" varchar DEFAULT 'Deutschland',
      "address_country_code" varchar DEFAULT 'DE',
      "geo_latitude" numeric,
      "geo_longitude" numeric,
      "local_business_enabled" boolean DEFAULT false,
      "local_business_type" varchar,
      "local_business_price_range" varchar,
      "robots_allow_indexing" boolean DEFAULT true,
      "verification_google" varchar,
      "verification_bing" varchar,
      "verification_yandex" varchar,
      "updated_at" timestamp(3) with time zone,
      "created_at" timestamp(3) with time zone
    );

    ALTER TABLE "seo_settings" DROP CONSTRAINT IF EXISTS "seo_settings_meta_defaults_default_og_image_id_fk";
    ALTER TABLE "seo_settings" ADD CONSTRAINT "seo_settings_meta_defaults_default_og_image_id_fk" FOREIGN KEY ("meta_defaults_default_og_image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
    ALTER TABLE "seo_settings" DROP CONSTRAINT IF EXISTS "seo_settings_organization_logo_id_fk";
    ALTER TABLE "seo_settings" ADD CONSTRAINT "seo_settings_organization_logo_id_fk" FOREIGN KEY ("organization_logo_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;

    -- SEO Settings Keywords (hasMany text)
    CREATE TABLE IF NOT EXISTS "seo_settings_meta_defaults_keywords" (
      "id" serial PRIMARY KEY NOT NULL,
      "_parent_id" integer NOT NULL,
      "order" integer NOT NULL,
      "value" varchar
    );

    CREATE INDEX IF NOT EXISTS "seo_settings_meta_defaults_keywords_parent_idx" ON "seo_settings_meta_defaults_keywords" USING btree ("_parent_id");
    CREATE INDEX IF NOT EXISTS "seo_settings_meta_defaults_keywords_order_idx" ON "seo_settings_meta_defaults_keywords" USING btree ("order");

    ALTER TABLE "seo_settings_meta_defaults_keywords" DROP CONSTRAINT IF EXISTS "seo_settings_meta_defaults_keywords_parent_id_fk";
    ALTER TABLE "seo_settings_meta_defaults_keywords" ADD CONSTRAINT "seo_settings_meta_defaults_keywords_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."seo_settings"("id") ON DELETE cascade ON UPDATE no action;

    -- SEO Settings Social Profiles (array)
    CREATE TABLE IF NOT EXISTS "seo_settings_social_profiles" (
      "id" serial PRIMARY KEY NOT NULL,
      "_parent_id" integer NOT NULL,
      "_order" integer NOT NULL,
      "platform" varchar,
      "url" varchar
    );

    CREATE INDEX IF NOT EXISTS "seo_settings_social_profiles_parent_idx" ON "seo_settings_social_profiles" USING btree ("_parent_id");
    CREATE INDEX IF NOT EXISTS "seo_settings_social_profiles_order_idx" ON "seo_settings_social_profiles" USING btree ("_order");

    ALTER TABLE "seo_settings_social_profiles" DROP CONSTRAINT IF EXISTS "seo_settings_social_profiles_parent_id_fk";
    ALTER TABLE "seo_settings_social_profiles" ADD CONSTRAINT "seo_settings_social_profiles_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."seo_settings"("id") ON DELETE cascade ON UPDATE no action;

    -- SEO Settings Opening Hours (array)
    CREATE TABLE IF NOT EXISTS "seo_settings_local_business_opening_hours" (
      "id" serial PRIMARY KEY NOT NULL,
      "_parent_id" integer NOT NULL,
      "_order" integer NOT NULL,
      "specification" varchar
    );

    CREATE INDEX IF NOT EXISTS "seo_settings_local_business_opening_hours_parent_idx" ON "seo_settings_local_business_opening_hours" USING btree ("_parent_id");
    CREATE INDEX IF NOT EXISTS "seo_settings_local_business_opening_hours_order_idx" ON "seo_settings_local_business_opening_hours" USING btree ("_order");

    ALTER TABLE "seo_settings_local_business_opening_hours" DROP CONSTRAINT IF EXISTS "seo_settings_local_business_opening_hours_parent_id_fk";
    ALTER TABLE "seo_settings_local_business_opening_hours" ADD CONSTRAINT "seo_settings_local_business_opening_hours_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."seo_settings"("id") ON DELETE cascade ON UPDATE no action;

    -- SEO Settings Additional Disallow Paths (hasMany text)
    CREATE TABLE IF NOT EXISTS "seo_settings_robots_additional_disallow" (
      "id" serial PRIMARY KEY NOT NULL,
      "_parent_id" integer NOT NULL,
      "order" integer NOT NULL,
      "value" varchar
    );

    CREATE INDEX IF NOT EXISTS "seo_settings_robots_additional_disallow_parent_idx" ON "seo_settings_robots_additional_disallow" USING btree ("_parent_id");
    CREATE INDEX IF NOT EXISTS "seo_settings_robots_additional_disallow_order_idx" ON "seo_settings_robots_additional_disallow" USING btree ("order");

    ALTER TABLE "seo_settings_robots_additional_disallow" DROP CONSTRAINT IF EXISTS "seo_settings_robots_additional_disallow_parent_id_fk";
    ALTER TABLE "seo_settings_robots_additional_disallow" ADD CONSTRAINT "seo_settings_robots_additional_disallow_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."seo_settings"("id") ON DELETE cascade ON UPDATE no action;
  `)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
    DROP TABLE IF EXISTS "pages_blocks_posts_list_block" CASCADE;
    DROP TABLE IF EXISTS "pages_blocks_testimonials_block" CASCADE;
    DROP TABLE IF EXISTS "pages_blocks_newsletter_block_available_interests" CASCADE;
    DROP TABLE IF EXISTS "pages_blocks_newsletter_block" CASCADE;
    DROP TABLE IF EXISTS "pages_blocks_process_steps_block_steps" CASCADE;
    DROP TABLE IF EXISTS "pages_blocks_process_steps_block" CASCADE;
    DROP TABLE IF EXISTS "seo_settings_meta_defaults_keywords" CASCADE;
    DROP TABLE IF EXISTS "seo_settings_social_profiles" CASCADE;
    DROP TABLE IF EXISTS "seo_settings_local_business_opening_hours" CASCADE;
    DROP TABLE IF EXISTS "seo_settings_robots_additional_disallow" CASCADE;
    DROP TABLE IF EXISTS "seo_settings" CASCADE;
  `)
}
