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

/**
 * Migration: Team Extensions
 *
 * Erweitert die Team Collection um:
 * - Slug für Einzelseiten
 * - Hierarchie-Felder (reportsTo, hierarchyLevel) für Organigramm
 * - vCard-Export-Flag
 *
 * Erstellt neue Blocks:
 * - TeamFilterBlock
 * - OrgChartBlock
 */

export async function up({ db }: MigrateUpArgs): Promise<void> {
  // ========================================
  // Team Collection Extensions
  // ========================================

  // Add slug field
  await db.execute(sql`
    ALTER TABLE "team" ADD COLUMN IF NOT EXISTS "slug" varchar UNIQUE;

    CREATE INDEX IF NOT EXISTS "team_slug_idx"
      ON "team" USING btree ("slug");
  `)

  // Add hierarchy fields
  await db.execute(sql`
    DO $$
    BEGIN
      CREATE TYPE "enum_team_hierarchy_level" AS ENUM('executive', 'department_head', 'team_lead', 'employee', 'trainee');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    ALTER TABLE "team" ADD COLUMN IF NOT EXISTS "reports_to_id" integer;
    ALTER TABLE "team" ADD COLUMN IF NOT EXISTS "hierarchy_level" "enum_team_hierarchy_level";
    ALTER TABLE "team" ADD COLUMN IF NOT EXISTS "allow_v_card" boolean DEFAULT true;

    ALTER TABLE "team"
      ADD CONSTRAINT "team_reports_to_id_fk"
      FOREIGN KEY ("reports_to_id") REFERENCES "public"."team"("id") ON DELETE set null ON UPDATE no action;

    CREATE INDEX IF NOT EXISTS "team_reports_to_idx"
      ON "team" USING btree ("reports_to_id");
    CREATE INDEX IF NOT EXISTS "team_hierarchy_level_idx"
      ON "team" USING btree ("hierarchy_level");
  `)

  // ========================================
  // TeamFilterBlock
  // ========================================
  await db.execute(sql`
    -- Enums for TeamFilterBlock
    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_team_filter_filter_layout" AS ENUM('horizontal', 'sidebar', 'dropdown', 'tabs');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_team_filter_filter_style" AS ENUM('buttons', 'select', 'checkbox');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_team_filter_display_layout" AS ENUM('grid', 'list', 'compact');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

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

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_team_filter_load_more" AS ENUM('button', 'infinite', 'pagination', 'all');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_team_filter_card_img_style" AS ENUM('circle', 'rounded', 'square');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_team_filter_style_bg" AS ENUM('none', 'light', 'dark');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

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

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_team_filter_style_hover" AS ENUM('none', 'lift', 'shadow', 'border');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_team_filter_style_gap" AS ENUM('16', '24', '32');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_team_filter_style_anim" AS ENUM('none', 'fade', 'slide', 'scale');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;
  `)

  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "pages_blocks_team_filter_block" (
      "_order" integer NOT NULL,
      "_parent_id" integer NOT NULL,
      "_path" text NOT NULL,
      "id" varchar PRIMARY KEY NOT NULL,
      -- Filters
      "filters_show_search" boolean DEFAULT true,
      "filters_show_department" boolean DEFAULT true,
      "filters_show_specialization" boolean DEFAULT false,
      "filters_show_language" boolean DEFAULT false,
      "filters_show_hierarchy" boolean DEFAULT false,
      "filters_filter_layout" "enum_pages_blocks_team_filter_filter_layout" DEFAULT 'horizontal',
      "filters_filter_style" "enum_pages_blocks_team_filter_filter_style" DEFAULT 'buttons',
      "filters_show_result_count" boolean DEFAULT true,
      "filters_show_reset_button" boolean DEFAULT true,
      -- Display
      "display_layout" "enum_pages_blocks_team_filter_display_layout" DEFAULT 'grid',
      "display_columns" "enum_pages_blocks_team_filter_display_cols" DEFAULT '3',
      "display_initial_limit" integer DEFAULT 12,
      "display_load_more" "enum_pages_blocks_team_filter_load_more" DEFAULT 'button',
      -- Card
      "card_show_image" boolean DEFAULT true,
      "card_image_style" "enum_pages_blocks_team_filter_card_img_style" DEFAULT 'circle',
      "card_show_role" boolean DEFAULT true,
      "card_show_department" boolean DEFAULT true,
      "card_show_bio" boolean DEFAULT false,
      "card_show_contact" boolean DEFAULT false,
      "card_show_social" boolean DEFAULT true,
      "card_show_specializations" boolean DEFAULT false,
      "card_show_languages" boolean DEFAULT false,
      "card_show_v_card" boolean DEFAULT false,
      "card_link_to_profile" boolean DEFAULT true,
      "card_profile_base_path" varchar DEFAULT '/team',
      "card_enable_modal" boolean DEFAULT false,
      -- Style
      "style_bg" "enum_pages_blocks_team_filter_style_bg" DEFAULT 'none',
      "style_card_bg" "enum_pages_blocks_team_filter_style_card_bg" DEFAULT 'white',
      "style_card_shadow" boolean DEFAULT true,
      "style_card_hover" "enum_pages_blocks_team_filter_style_hover" DEFAULT 'lift',
      "style_gap" "enum_pages_blocks_team_filter_style_gap" DEFAULT '24',
      "style_animation" "enum_pages_blocks_team_filter_style_anim" DEFAULT 'fade',
      -- Empty state
      "empty_state_show_reset_button" boolean DEFAULT true,
      "block_name" varchar
    );

    CREATE TABLE IF NOT EXISTS "pages_blocks_team_filter_block_locales" (
      "title" varchar DEFAULT 'Unser Team',
      "subtitle" varchar,
      "filters_search_placeholder" varchar DEFAULT 'Team durchsuchen...',
      "display_load_more_text" varchar DEFAULT 'Mehr anzeigen',
      "empty_state_title" varchar DEFAULT 'Keine Mitarbeiter gefunden',
      "empty_state_message" varchar DEFAULT 'Versuchen Sie andere Filterkriterien.',
      "id" serial PRIMARY KEY NOT NULL,
      "_locale" "_locales" NOT NULL,
      "_parent_id" varchar NOT NULL
    );

    ALTER TABLE "pages_blocks_team_filter_block"
      ADD CONSTRAINT "pages_blocks_team_filter_block_parent_id_fk"
      FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;

    ALTER TABLE "pages_blocks_team_filter_block_locales"
      ADD CONSTRAINT "pages_blocks_team_filter_block_locales_parent_id_fk"
      FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_team_filter_block"("id") ON DELETE cascade ON UPDATE no action;

    CREATE INDEX IF NOT EXISTS "pages_blocks_team_filter_block_order_idx"
      ON "pages_blocks_team_filter_block" USING btree ("_order");
    CREATE INDEX IF NOT EXISTS "pages_blocks_team_filter_block_parent_id_idx"
      ON "pages_blocks_team_filter_block" USING btree ("_parent_id");
    CREATE INDEX IF NOT EXISTS "pages_blocks_team_filter_block_path_idx"
      ON "pages_blocks_team_filter_block" USING btree ("_path");

    CREATE UNIQUE INDEX IF NOT EXISTS "pages_blocks_team_filter_block_locales_locale_parent_id_unique"
      ON "pages_blocks_team_filter_block_locales" USING btree ("_locale", "_parent_id");
  `)

  // ========================================
  // OrgChartBlock
  // ========================================
  await db.execute(sql`
    -- Enums for OrgChartBlock
    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_source" AS ENUM('auto', 'department', 'manual');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_layout" AS ENUM('tree', 'tree-horizontal', 'org', 'radial', 'layers');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_direction" AS ENUM('top', 'bottom', 'left', 'right');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_node_style" AS ENUM('card', 'compact', 'avatar', 'text');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_node_img_size" AS ENUM('sm', 'md', 'lg');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_node_img_shape" AS ENUM('circle', 'rounded', 'square');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_node_click" AS ENUM('none', 'modal', 'link', 'expand');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_conn_style" AS ENUM('straight', 'angular', 'curved', 'dashed');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_conn_color" AS ENUM('gray', 'dark', 'accent', 'gradient');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_conn_thick" AS ENUM('1', '2', '3');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_style_bg" AS ENUM('none', 'light', 'white', 'dark', 'gradient');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

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

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_style_spacing" AS ENUM('sm', 'md', 'lg');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;

    DO $$
    BEGIN
      CREATE TYPE "enum_pages_blocks_org_chart_style_height" AS ENUM('auto', '300', '400', '500', '600', 'full');
    EXCEPTION WHEN duplicate_object THEN null;
    END $$;
  `)

  await db.execute(sql`
    CREATE TABLE IF NOT EXISTS "pages_blocks_org_chart_block" (
      "_order" integer NOT NULL,
      "_parent_id" integer NOT NULL,
      "_path" text NOT NULL,
      "id" varchar PRIMARY KEY NOT NULL,
      -- Source
      "source" "enum_pages_blocks_org_chart_source" DEFAULT 'auto',
      "root_member_id" integer,
      "department" varchar,
      "max_depth" integer DEFAULT 5,
      -- Layout
      "layout" "enum_pages_blocks_org_chart_layout" DEFAULT 'tree',
      "direction" "enum_pages_blocks_org_chart_direction" DEFAULT 'top',
      -- Node
      "node_style" "enum_pages_blocks_org_chart_node_style" DEFAULT 'card',
      "node_show_image" boolean DEFAULT true,
      "node_image_size" "enum_pages_blocks_org_chart_node_img_size" DEFAULT 'md',
      "node_image_shape" "enum_pages_blocks_org_chart_node_img_shape" DEFAULT 'circle',
      "node_show_name" boolean DEFAULT true,
      "node_show_role" boolean DEFAULT true,
      "node_show_department" boolean DEFAULT false,
      "node_show_contact" boolean DEFAULT false,
      "node_click_action" "enum_pages_blocks_org_chart_node_click" DEFAULT 'modal',
      "node_profile_base_path" varchar DEFAULT '/team',
      -- Connectors
      "connectors_style" "enum_pages_blocks_org_chart_conn_style" DEFAULT 'straight',
      "connectors_color" "enum_pages_blocks_org_chart_conn_color" DEFAULT 'gray',
      "connectors_thickness" "enum_pages_blocks_org_chart_conn_thick" DEFAULT '2',
      "connectors_animated" boolean DEFAULT false,
      -- Levels
      "levels_color_by_level" boolean DEFAULT true,
      "levels_size_by_level" boolean DEFAULT true,
      "levels_collapsible" boolean DEFAULT true,
      "levels_initially_expanded" integer DEFAULT 2,
      -- Interaction
      "interaction_zoomable" boolean DEFAULT true,
      "interaction_pannable" boolean DEFAULT true,
      "interaction_minimap" boolean DEFAULT false,
      "interaction_search" boolean DEFAULT false,
      "interaction_highlight" boolean DEFAULT true,
      "interaction_fullscreen" boolean DEFAULT true,
      "interaction_export" boolean DEFAULT false,
      -- Style
      "style_bg" "enum_pages_blocks_org_chart_style_bg" DEFAULT 'light',
      "style_node_bg" "enum_pages_blocks_org_chart_style_node_bg" DEFAULT 'white',
      "style_node_shadow" boolean DEFAULT true,
      "style_node_border" boolean DEFAULT false,
      "style_spacing" "enum_pages_blocks_org_chart_style_spacing" DEFAULT 'md',
      "style_min_height" "enum_pages_blocks_org_chart_style_height" DEFAULT '400',
      -- Other
      "show_legend" boolean DEFAULT false,
      "block_name" varchar
    );

    CREATE TABLE IF NOT EXISTS "pages_blocks_org_chart_block_locales" (
      "title" varchar DEFAULT 'Unsere Struktur',
      "subtitle" varchar,
      "a11y_label" varchar DEFAULT 'Organigramm der Unternehmensstruktur',
      "id" serial PRIMARY KEY NOT NULL,
      "_locale" "_locales" NOT NULL,
      "_parent_id" varchar NOT NULL
    );

    -- Selected members relationship
    CREATE TABLE IF NOT EXISTS "pages_blocks_org_chart_block_rels" (
      "id" serial PRIMARY KEY NOT NULL,
      "order" integer,
      "parent_id" varchar NOT NULL,
      "path" varchar NOT NULL,
      "team_id" integer
    );

    ALTER TABLE "pages_blocks_org_chart_block"
      ADD CONSTRAINT "pages_blocks_org_chart_block_parent_id_fk"
      FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;

    ALTER TABLE "pages_blocks_org_chart_block"
      ADD CONSTRAINT "pages_blocks_org_chart_block_root_member_id_fk"
      FOREIGN KEY ("root_member_id") REFERENCES "public"."team"("id") ON DELETE set null ON UPDATE no action;

    ALTER TABLE "pages_blocks_org_chart_block_locales"
      ADD CONSTRAINT "pages_blocks_org_chart_block_locales_parent_id_fk"
      FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_org_chart_block"("id") ON DELETE cascade ON UPDATE no action;

    ALTER TABLE "pages_blocks_org_chart_block_rels"
      ADD CONSTRAINT "pages_blocks_org_chart_block_rels_parent_fk"
      FOREIGN KEY ("parent_id") REFERENCES "public"."pages_blocks_org_chart_block"("id") ON DELETE cascade ON UPDATE no action;

    ALTER TABLE "pages_blocks_org_chart_block_rels"
      ADD CONSTRAINT "pages_blocks_org_chart_block_rels_team_fk"
      FOREIGN KEY ("team_id") REFERENCES "public"."team"("id") ON DELETE cascade ON UPDATE no action;

    CREATE INDEX IF NOT EXISTS "pages_blocks_org_chart_block_order_idx"
      ON "pages_blocks_org_chart_block" USING btree ("_order");
    CREATE INDEX IF NOT EXISTS "pages_blocks_org_chart_block_parent_id_idx"
      ON "pages_blocks_org_chart_block" USING btree ("_parent_id");
    CREATE INDEX IF NOT EXISTS "pages_blocks_org_chart_block_path_idx"
      ON "pages_blocks_org_chart_block" USING btree ("_path");

    CREATE UNIQUE INDEX IF NOT EXISTS "pages_blocks_org_chart_block_locales_locale_parent_id_unique"
      ON "pages_blocks_org_chart_block_locales" USING btree ("_locale", "_parent_id");
  `)
}

export async function down({ db }: MigrateDownArgs): Promise<void> {
  // Drop OrgChartBlock
  await db.execute(sql`
    DROP TABLE IF EXISTS "pages_blocks_org_chart_block_rels" CASCADE;
    DROP TABLE IF EXISTS "pages_blocks_org_chart_block_locales" CASCADE;
    DROP TABLE IF EXISTS "pages_blocks_org_chart_block" CASCADE;
  `)

  // Drop OrgChartBlock enums
  await db.execute(sql`
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_style_height";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_style_spacing";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_style_node_bg";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_style_bg";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_conn_thick";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_conn_color";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_conn_style";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_node_click";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_node_img_shape";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_node_img_size";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_node_style";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_direction";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_layout";
    DROP TYPE IF EXISTS "enum_pages_blocks_org_chart_source";
  `)

  // Drop TeamFilterBlock
  await db.execute(sql`
    DROP TABLE IF EXISTS "pages_blocks_team_filter_block_locales" CASCADE;
    DROP TABLE IF EXISTS "pages_blocks_team_filter_block" CASCADE;
  `)

  // Drop TeamFilterBlock enums
  await db.execute(sql`
    DROP TYPE IF EXISTS "enum_pages_blocks_team_filter_style_anim";
    DROP TYPE IF EXISTS "enum_pages_blocks_team_filter_style_gap";
    DROP TYPE IF EXISTS "enum_pages_blocks_team_filter_style_hover";
    DROP TYPE IF EXISTS "enum_pages_blocks_team_filter_style_card_bg";
    DROP TYPE IF EXISTS "enum_pages_blocks_team_filter_style_bg";
    DROP TYPE IF EXISTS "enum_pages_blocks_team_filter_card_img_style";
    DROP TYPE IF EXISTS "enum_pages_blocks_team_filter_load_more";
    DROP TYPE IF EXISTS "enum_pages_blocks_team_filter_display_cols";
    DROP TYPE IF EXISTS "enum_pages_blocks_team_filter_display_layout";
    DROP TYPE IF EXISTS "enum_pages_blocks_team_filter_filter_style";
    DROP TYPE IF EXISTS "enum_pages_blocks_team_filter_filter_layout";
  `)

  // Drop Team extensions
  await db.execute(sql`
    ALTER TABLE "team" DROP COLUMN IF EXISTS "slug";
    ALTER TABLE "team" DROP COLUMN IF EXISTS "reports_to_id";
    ALTER TABLE "team" DROP COLUMN IF EXISTS "hierarchy_level";
    ALTER TABLE "team" DROP COLUMN IF EXISTS "allow_v_card";
    DROP TYPE IF EXISTS "enum_team_hierarchy_level";
  `)
}
