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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  // Create enums for locations
  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_locations_hours_structured_day" AS ENUM('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_locations_type" AS ENUM('headquarters', 'office', 'branch', 'warehouse', 'showroom', 'studio', 'practice', 'workshop');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  // Create enums for partners
  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_partners_type" AS ENUM('partner', 'client', 'reference', 'sponsor', 'certification', 'membership', 'award', 'supplier', 'technology');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  // Create enums for jobs
  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_jobs_benefits_list_icon" AS ENUM('money', 'vacation', 'home', 'education', 'health', 'team', 'flexible', 'transport', 'food', 'childcare', 'fitness', 'parking');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_jobs_category" AS ENUM('care', 'medical', 'admin', 'it', 'marketing', 'sales', 'finance', 'hr', 'production', 'logistics', 'other');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_jobs_type" AS ENUM('fulltime', 'parttime', 'minijob', 'working_student', 'internship', 'apprenticeship', 'dual_study', 'freelance', 'temporary');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_jobs_work_model" AS ENUM('onsite', 'remote', 'hybrid');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_jobs_experience_level" AS ENUM('entry', 'experienced', 'senior', 'management');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_jobs_salary_type" AS ENUM('yearly', 'monthly', 'hourly');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_jobs_application_method" AS ENUM('email', 'form', 'external', 'mail');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_jobs_status" AS ENUM('draft', 'published', 'filled', 'archived');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  // Create enums for downloads
  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_downloads_access_form_fields" AS ENUM('name', 'email', 'company', 'phone');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_downloads_file_type" AS ENUM('pdf', 'doc', 'xls', 'ppt', 'image', 'video', 'audio', 'archive', 'other');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  await db.execute(sql`
    DO $$ BEGIN
      CREATE TYPE "public"."enum_downloads_category" AS ENUM('brochure', 'flyer', 'catalog', 'pricelist', 'form', 'manual', 'datasheet', 'certificate', 'press', 'whitepaper', 'presentation', 'legal', 'other');
    EXCEPTION
      WHEN duplicate_object THEN null;
    END $$;
  `)

  // Create locations table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "locations" (
      "id" serial PRIMARY KEY NOT NULL,
      "tenant_id" integer REFERENCES "tenants"("id") ON DELETE SET NULL,
      "slug" varchar NOT NULL,
      "type" "enum_locations_type" DEFAULT 'office',
      "image_id" integer REFERENCES "media"("id") ON DELETE SET NULL,
      "address_street" varchar NOT NULL,
      "address_additional_line" varchar,
      "address_zip" varchar NOT NULL,
      "address_city" varchar NOT NULL,
      "address_state" varchar,
      "address_country" varchar DEFAULT 'Deutschland',
      "geo_lat" numeric,
      "geo_lng" numeric,
      "geo_zoom" numeric DEFAULT 15,
      "contact_phone" varchar,
      "contact_fax" varchar,
      "contact_email" varchar,
      "contact_website" varchar,
      "is_main" boolean DEFAULT false,
      "is_active" boolean DEFAULT true,
      "show_in_footer" boolean DEFAULT false,
      "order" numeric DEFAULT 0,
      "updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
      "created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
    );
  `)

  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "locations_tenant_idx" ON "locations" USING btree ("tenant_id");
  `)
  await db.execute(sql`
    CREATE UNIQUE INDEX IF NOT EXISTS "locations_slug_idx" ON "locations" USING btree ("slug");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "locations_image_idx" ON "locations" USING btree ("image_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "locations_updated_at_idx" ON "locations" USING btree ("updated_at");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "locations_created_at_idx" ON "locations" USING btree ("created_at");
  `)

  // Create locations_locales table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "locations_locales" (
      "name" varchar NOT NULL,
      "description" jsonb,
      "hours_display" varchar,
      "hours_note" varchar,
      "directions_text" jsonb,
      "directions_parking" varchar,
      "directions_public_transport" varchar,
      "directions_accessibility" varchar,
      "id" serial PRIMARY KEY NOT NULL,
      "_locale" "_locales" NOT NULL,
      "_parent_id" integer NOT NULL REFERENCES "locations"("id") ON DELETE CASCADE
    );
  `)

  await db.execute(sql`
    CREATE UNIQUE INDEX IF NOT EXISTS "locations_locales_locale_parent_id_unique" ON "locations_locales" USING btree ("_locale", "_parent_id");
  `)

  // Create locations_rels table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "locations_rels" (
      "id" serial PRIMARY KEY NOT NULL,
      "order" integer,
      "parent_id" integer NOT NULL REFERENCES "locations"("id") ON DELETE CASCADE,
      "path" varchar NOT NULL,
      "services_id" integer REFERENCES "services"("id") ON DELETE CASCADE,
      "team_id" integer REFERENCES "team"("id") ON DELETE CASCADE
    );
  `)

  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "locations_rels_order_idx" ON "locations_rels" USING btree ("order");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "locations_rels_parent_idx" ON "locations_rels" USING btree ("parent_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "locations_rels_path_idx" ON "locations_rels" USING btree ("path");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "locations_rels_services_id_idx" ON "locations_rels" USING btree ("services_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "locations_rels_team_id_idx" ON "locations_rels" USING btree ("team_id");
  `)

  // Create partners table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "partners" (
      "id" serial PRIMARY KEY NOT NULL,
      "tenant_id" integer REFERENCES "tenants"("id") ON DELETE SET NULL,
      "name" varchar NOT NULL,
      "slug" varchar NOT NULL,
      "type" "enum_partners_type" NOT NULL DEFAULT 'partner',
      "logo_id" integer NOT NULL REFERENCES "media"("id") ON DELETE SET NULL,
      "logo_light_id" integer REFERENCES "media"("id") ON DELETE SET NULL,
      "website" varchar,
      "case_study_quote_person" varchar,
      "case_study_quote_role" varchar,
      "certification_issuer" varchar,
      "certification_valid_from" timestamp(3) with time zone,
      "certification_valid_until" timestamp(3) with time zone,
      "certification_cert_number" varchar,
      "certification_document_id" integer REFERENCES "media"("id") ON DELETE SET NULL,
      "category" varchar,
      "is_featured" boolean DEFAULT false,
      "is_active" boolean DEFAULT true,
      "order" numeric DEFAULT 0,
      "since" timestamp(3) with time zone,
      "updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
      "created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
    );
  `)

  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "partners_tenant_idx" ON "partners" USING btree ("tenant_id");
  `)
  await db.execute(sql`
    CREATE UNIQUE INDEX IF NOT EXISTS "partners_slug_idx" ON "partners" USING btree ("slug");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "partners_logo_idx" ON "partners" USING btree ("logo_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "partners_logo_light_idx" ON "partners" USING btree ("logo_light_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "partners_certification_certification_document_idx" ON "partners" USING btree ("certification_document_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "partners_updated_at_idx" ON "partners" USING btree ("updated_at");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "partners_created_at_idx" ON "partners" USING btree ("created_at");
  `)

  // Create partners_locales table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "partners_locales" (
      "description" varchar,
      "case_study_quote" varchar,
      "case_study_project_description" jsonb,
      "id" serial PRIMARY KEY NOT NULL,
      "_locale" "_locales" NOT NULL,
      "_parent_id" integer NOT NULL REFERENCES "partners"("id") ON DELETE CASCADE
    );
  `)

  await db.execute(sql`
    CREATE UNIQUE INDEX IF NOT EXISTS "partners_locales_locale_parent_id_unique" ON "partners_locales" USING btree ("_locale", "_parent_id");
  `)

  // Create partners_case_study_results table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "partners_case_study_results" (
      "_order" integer NOT NULL,
      "_parent_id" integer NOT NULL REFERENCES "partners"("id") ON DELETE CASCADE,
      "id" varchar PRIMARY KEY NOT NULL,
      "metric" varchar
    );
  `)

  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "partners_case_study_results_order_idx" ON "partners_case_study_results" USING btree ("_order");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "partners_case_study_results_parent_id_idx" ON "partners_case_study_results" USING btree ("_parent_id");
  `)

  // Create partners_case_study_results_locales table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "partners_case_study_results_locales" (
      "label" varchar,
      "id" serial PRIMARY KEY NOT NULL,
      "_locale" "_locales" NOT NULL,
      "_parent_id" varchar NOT NULL REFERENCES "partners_case_study_results"("id") ON DELETE CASCADE
    );
  `)

  await db.execute(sql`
    CREATE UNIQUE INDEX IF NOT EXISTS "partners_case_study_results_locales_locale_parent_id_unique" ON "partners_case_study_results_locales" USING btree ("_locale", "_parent_id");
  `)

  // Create partners_tags table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "partners_tags" (
      "_order" integer NOT NULL,
      "_parent_id" integer NOT NULL REFERENCES "partners"("id") ON DELETE CASCADE,
      "id" varchar PRIMARY KEY NOT NULL,
      "tag" varchar NOT NULL
    );
  `)

  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "partners_tags_order_idx" ON "partners_tags" USING btree ("_order");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "partners_tags_parent_id_idx" ON "partners_tags" USING btree ("_parent_id");
  `)

  // Create jobs table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "jobs" (
      "id" serial PRIMARY KEY NOT NULL,
      "tenant_id" integer REFERENCES "tenants"("id") ON DELETE SET NULL,
      "slug" varchar NOT NULL,
      "reference" varchar,
      "category" "enum_jobs_category",
      "type" "enum_jobs_type" NOT NULL DEFAULT 'fulltime',
      "work_model" "enum_jobs_work_model",
      "experience_level" "enum_jobs_experience_level",
      "location_location_ref_id" integer REFERENCES "locations"("id") ON DELETE SET NULL,
      "location_city" varchar,
      "location_region" varchar,
      "location_country" varchar DEFAULT 'Deutschland',
      "salary_show" boolean DEFAULT false,
      "salary_type" "enum_jobs_salary_type",
      "salary_min" numeric,
      "salary_max" numeric,
      "salary_currency" varchar DEFAULT 'EUR',
      "application_method" "enum_jobs_application_method" DEFAULT 'email',
      "application_email" varchar,
      "application_form_id_id" integer REFERENCES "forms"("id") ON DELETE SET NULL,
      "application_external_url" varchar,
      "application_contact_id" integer REFERENCES "team"("id") ON DELETE SET NULL,
      "application_deadline" timestamp(3) with time zone,
      "image_id" integer REFERENCES "media"("id") ON DELETE SET NULL,
      "status" "enum_jobs_status" NOT NULL DEFAULT 'draft',
      "published_at" timestamp(3) with time zone,
      "is_featured" boolean DEFAULT false,
      "is_urgent" boolean DEFAULT false,
      "updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
      "created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
    );
  `)

  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "jobs_tenant_idx" ON "jobs" USING btree ("tenant_id");
  `)
  await db.execute(sql`
    CREATE UNIQUE INDEX IF NOT EXISTS "jobs_slug_idx" ON "jobs" USING btree ("slug");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "jobs_location_location_location_ref_idx" ON "jobs" USING btree ("location_location_ref_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "jobs_application_application_form_id_idx" ON "jobs" USING btree ("application_form_id_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "jobs_application_application_contact_idx" ON "jobs" USING btree ("application_contact_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "jobs_image_idx" ON "jobs" USING btree ("image_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "jobs_updated_at_idx" ON "jobs" USING btree ("updated_at");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "jobs_created_at_idx" ON "jobs" USING btree ("created_at");
  `)

  // Create jobs_locales table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "jobs_locales" (
      "title" varchar NOT NULL,
      "department" varchar,
      "summary" varchar,
      "description" jsonb,
      "responsibilities" jsonb,
      "requirements" jsonb,
      "qualifications" jsonb,
      "benefits" jsonb,
      "salary_note" varchar,
      "seo_meta_title" varchar,
      "seo_meta_description" varchar,
      "id" serial PRIMARY KEY NOT NULL,
      "_locale" "_locales" NOT NULL,
      "_parent_id" integer NOT NULL REFERENCES "jobs"("id") ON DELETE CASCADE
    );
  `)

  await db.execute(sql`
    CREATE UNIQUE INDEX IF NOT EXISTS "jobs_locales_locale_parent_id_unique" ON "jobs_locales" USING btree ("_locale", "_parent_id");
  `)

  // Create jobs_benefits_list table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "jobs_benefits_list" (
      "_order" integer NOT NULL,
      "_parent_id" integer NOT NULL REFERENCES "jobs"("id") ON DELETE CASCADE,
      "id" varchar PRIMARY KEY NOT NULL,
      "icon" "enum_jobs_benefits_list_icon"
    );
  `)

  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "jobs_benefits_list_order_idx" ON "jobs_benefits_list" USING btree ("_order");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "jobs_benefits_list_parent_id_idx" ON "jobs_benefits_list" USING btree ("_parent_id");
  `)

  // Create jobs_benefits_list_locales table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "jobs_benefits_list_locales" (
      "benefit" varchar NOT NULL,
      "id" serial PRIMARY KEY NOT NULL,
      "_locale" "_locales" NOT NULL,
      "_parent_id" varchar NOT NULL REFERENCES "jobs_benefits_list"("id") ON DELETE CASCADE
    );
  `)

  await db.execute(sql`
    CREATE UNIQUE INDEX IF NOT EXISTS "jobs_benefits_list_locales_locale_parent_id_unique" ON "jobs_benefits_list_locales" USING btree ("_locale", "_parent_id");
  `)

  // Create downloads table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "downloads" (
      "id" serial PRIMARY KEY NOT NULL,
      "tenant_id" integer REFERENCES "tenants"("id") ON DELETE SET NULL,
      "slug" varchar NOT NULL,
      "file_id" integer NOT NULL REFERENCES "media"("id") ON DELETE SET NULL,
      "file_type" "enum_downloads_file_type",
      "file_size" varchar,
      "category" "enum_downloads_category",
      "thumbnail_id" integer REFERENCES "media"("id") ON DELETE SET NULL,
      "access_require_login" boolean DEFAULT false,
      "access_require_form" boolean DEFAULT false,
      "download_count" numeric DEFAULT 0,
      "version" varchar,
      "last_updated" timestamp(3) with time zone,
      "is_active" boolean DEFAULT true,
      "is_featured" boolean DEFAULT false,
      "order" numeric DEFAULT 0,
      "updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
      "created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
    );
  `)

  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_tenant_idx" ON "downloads" USING btree ("tenant_id");
  `)
  await db.execute(sql`
    CREATE UNIQUE INDEX IF NOT EXISTS "downloads_slug_idx" ON "downloads" USING btree ("slug");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_file_idx" ON "downloads" USING btree ("file_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_thumbnail_idx" ON "downloads" USING btree ("thumbnail_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_updated_at_idx" ON "downloads" USING btree ("updated_at");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_created_at_idx" ON "downloads" USING btree ("created_at");
  `)

  // Create downloads_locales table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "downloads_locales" (
      "title" varchar NOT NULL,
      "description" varchar,
      "id" serial PRIMARY KEY NOT NULL,
      "_locale" "_locales" NOT NULL,
      "_parent_id" integer NOT NULL REFERENCES "downloads"("id") ON DELETE CASCADE
    );
  `)

  await db.execute(sql`
    CREATE UNIQUE INDEX IF NOT EXISTS "downloads_locales_locale_parent_id_unique" ON "downloads_locales" USING btree ("_locale", "_parent_id");
  `)

  // Create downloads_tags table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "downloads_tags" (
      "_order" integer NOT NULL,
      "_parent_id" integer NOT NULL REFERENCES "downloads"("id") ON DELETE CASCADE,
      "id" varchar PRIMARY KEY NOT NULL,
      "tag" varchar NOT NULL
    );
  `)

  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_tags_order_idx" ON "downloads_tags" USING btree ("_order");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_tags_parent_id_idx" ON "downloads_tags" USING btree ("_parent_id");
  `)

  // Create downloads_access_form_fields table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "downloads_access_form_fields" (
      "order" integer NOT NULL,
      "parent_id" integer NOT NULL REFERENCES "downloads"("id") ON DELETE CASCADE,
      "value" "enum_downloads_access_form_fields",
      "id" serial PRIMARY KEY NOT NULL
    );
  `)

  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_access_form_fields_order_idx" ON "downloads_access_form_fields" USING btree ("order");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_access_form_fields_parent_idx" ON "downloads_access_form_fields" USING btree ("parent_id");
  `)

  // Create downloads_rels table
  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "downloads_rels" (
      "id" serial PRIMARY KEY NOT NULL,
      "order" integer,
      "parent_id" integer NOT NULL REFERENCES "downloads"("id") ON DELETE CASCADE,
      "path" varchar NOT NULL,
      "services_id" integer REFERENCES "services"("id") ON DELETE CASCADE,
      "products_id" integer REFERENCES "products"("id") ON DELETE CASCADE
    );
  `)

  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_rels_order_idx" ON "downloads_rels" USING btree ("order");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_rels_parent_idx" ON "downloads_rels" USING btree ("parent_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_rels_path_idx" ON "downloads_rels" USING btree ("path");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_rels_services_id_idx" ON "downloads_rels" USING btree ("services_id");
  `)
  await db.execute(sql`
    CREATE INDEX IF NOT EXISTS "downloads_rels_products_id_idx" ON "downloads_rels" USING btree ("products_id");
  `)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  // Drop tables in reverse order (respecting foreign key constraints)
  await db.execute(sql`DROP TABLE IF EXISTS "downloads_rels" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "downloads_access_form_fields" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "downloads_tags" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "downloads_locales" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "downloads" CASCADE;`)

  await db.execute(sql`DROP TABLE IF EXISTS "jobs_benefits_list_locales" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "jobs_benefits_list" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "jobs_locales" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "jobs" CASCADE;`)

  await db.execute(sql`DROP TABLE IF EXISTS "partners_tags" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "partners_case_study_results_locales" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "partners_case_study_results" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "partners_locales" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "partners" CASCADE;`)

  await db.execute(sql`DROP TABLE IF EXISTS "locations_rels" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "locations_locales" CASCADE;`)
  await db.execute(sql`DROP TABLE IF EXISTS "locations" CASCADE;`)

  // Drop enums
  await db.execute(sql`DROP TYPE IF EXISTS "enum_downloads_category" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_downloads_file_type" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_downloads_access_form_fields" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_jobs_status" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_jobs_application_method" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_jobs_salary_type" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_jobs_experience_level" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_jobs_work_model" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_jobs_type" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_jobs_category" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_jobs_benefits_list_icon" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_partners_type" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_locations_type" CASCADE;`)
  await db.execute(sql`DROP TYPE IF EXISTS "enum_locations_hours_structured_day" CASCADE;`)
}
