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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
    -- Neue Felder für Media Collection
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "caption" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "credit" varchar;

    -- Image Size Spalten für optimierte Bildvarianten
    -- WebP Varianten
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_thumbnail_url" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_thumbnail_width" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_thumbnail_height" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_thumbnail_mime_type" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_thumbnail_filesize" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_thumbnail_filename" varchar;

    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_small_url" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_small_width" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_small_height" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_small_mime_type" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_small_filesize" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_small_filename" varchar;

    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_medium_url" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_medium_width" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_medium_height" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_medium_mime_type" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_medium_filesize" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_medium_filename" varchar;

    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_large_url" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_large_width" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_large_height" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_large_mime_type" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_large_filesize" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_large_filename" varchar;

    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_xlarge_url" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_xlarge_width" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_xlarge_height" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_xlarge_mime_type" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_xlarge_filesize" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_xlarge_filename" varchar;

    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_2k_url" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_2k_width" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_2k_height" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_2k_mime_type" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_2k_filesize" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_2k_filename" varchar;

    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_og_url" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_og_width" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_og_height" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_og_mime_type" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_og_filesize" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_og_filename" varchar;

    -- AVIF Varianten
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_medium_avif_url" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_medium_avif_width" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_medium_avif_height" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_medium_avif_mime_type" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_medium_avif_filesize" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_medium_avif_filename" varchar;

    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_large_avif_url" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_large_avif_width" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_large_avif_height" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_large_avif_mime_type" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_large_avif_filesize" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_large_avif_filename" varchar;

    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_xlarge_avif_url" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_xlarge_avif_width" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_xlarge_avif_height" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_xlarge_avif_mime_type" varchar;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_xlarge_avif_filesize" numeric;
    ALTER TABLE "media" ADD COLUMN IF NOT EXISTS "sizes_xlarge_avif_filename" varchar;

    -- Tags Tabelle (hasMany text)
    CREATE TABLE IF NOT EXISTS "media_tags" (
      "order" integer NOT NULL,
      "parent_id" integer NOT NULL,
      "value" varchar,
      "id" serial PRIMARY KEY NOT NULL
    );

    CREATE INDEX IF NOT EXISTS "media_tags_order_idx" ON "media_tags" USING btree ("order");
    CREATE INDEX IF NOT EXISTS "media_tags_parent_idx" ON "media_tags" USING btree ("parent_id");

    ALTER TABLE "media_tags" DROP CONSTRAINT IF EXISTS "media_tags_parent_id_fk";
    ALTER TABLE "media_tags" ADD CONSTRAINT "media_tags_parent_id_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."media"("id") ON DELETE cascade ON UPDATE no action;
  `)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
    ALTER TABLE "media" DROP COLUMN IF EXISTS "caption";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "credit";

    -- WebP Sizes
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_thumbnail_url";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_thumbnail_width";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_thumbnail_height";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_thumbnail_mime_type";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_thumbnail_filesize";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_thumbnail_filename";

    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_small_url";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_small_width";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_small_height";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_small_mime_type";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_small_filesize";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_small_filename";

    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_medium_url";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_medium_width";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_medium_height";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_medium_mime_type";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_medium_filesize";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_medium_filename";

    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_large_url";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_large_width";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_large_height";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_large_mime_type";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_large_filesize";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_large_filename";

    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_xlarge_url";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_xlarge_width";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_xlarge_height";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_xlarge_mime_type";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_xlarge_filesize";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_xlarge_filename";

    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_2k_url";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_2k_width";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_2k_height";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_2k_mime_type";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_2k_filesize";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_2k_filename";

    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_og_url";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_og_width";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_og_height";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_og_mime_type";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_og_filesize";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_og_filename";

    -- AVIF Sizes
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_medium_avif_url";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_medium_avif_width";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_medium_avif_height";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_medium_avif_mime_type";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_medium_avif_filesize";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_medium_avif_filename";

    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_large_avif_url";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_large_avif_width";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_large_avif_height";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_large_avif_mime_type";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_large_avif_filesize";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_large_avif_filename";

    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_xlarge_avif_url";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_xlarge_avif_width";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_xlarge_avif_height";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_xlarge_avif_mime_type";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_xlarge_avif_filesize";
    ALTER TABLE "media" DROP COLUMN IF EXISTS "sizes_xlarge_avif_filename";

    DROP TABLE IF EXISTS "media_tags" CASCADE;
  `)
}
