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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TYPE "public"."enum_pages_blocks_team_block_display_mode" AS ENUM('all', 'featured', 'department', 'selected');
  CREATE TYPE "public"."enum_pages_blocks_team_block_layout" AS ENUM('grid', 'list', 'slider', 'compact', 'detailed');
  CREATE TYPE "public"."enum_pages_blocks_team_block_columns" AS ENUM('2', '3', '4');
  CREATE TYPE "public"."enum_pages_blocks_team_block_image_style" AS ENUM('circle', 'rounded', 'square');
  CREATE TYPE "public"."enum_pages_blocks_team_block_background_color" AS ENUM('white', 'light', 'dark', 'accent');
  CREATE TYPE "public"."enum_team_social_links_platform" AS ENUM('linkedin', 'xing', 'twitter', 'instagram', 'facebook', 'website', 'github');
  CREATE TYPE "public"."enum_team_languages_level" AS ENUM('native', 'fluent', 'good', 'basic');
  CREATE TABLE "pages_blocks_team_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"display_mode" "enum_pages_blocks_team_block_display_mode" DEFAULT 'all',
  	"department" varchar,
  	"limit" numeric DEFAULT 12,
  	"layout" "enum_pages_blocks_team_block_layout" DEFAULT 'grid',
  	"columns" "enum_pages_blocks_team_block_columns" DEFAULT '3',
  	"show_role" boolean DEFAULT true,
  	"show_department" boolean DEFAULT false,
  	"show_bio" boolean DEFAULT true,
  	"show_contact" boolean DEFAULT false,
  	"show_social_links" boolean DEFAULT true,
  	"show_qualifications" boolean DEFAULT false,
  	"show_specializations" boolean DEFAULT false,
  	"show_languages" boolean DEFAULT false,
  	"group_by_department" boolean DEFAULT false,
  	"autoplay" boolean DEFAULT true,
  	"autoplay_speed" numeric DEFAULT 5000,
  	"enable_detail_view" boolean DEFAULT false,
  	"image_style" "enum_pages_blocks_team_block_image_style" DEFAULT 'rounded',
  	"background_color" "enum_pages_blocks_team_block_background_color" DEFAULT 'white',
  	"show_c_t_a" boolean DEFAULT false,
  	"cta_link" varchar DEFAULT '/karriere',
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_team_block_locales" (
  	"title" varchar DEFAULT 'Unser Team',
  	"subtitle" varchar,
  	"introduction" jsonb,
  	"cta_text" varchar DEFAULT 'Jetzt bewerben',
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "team_social_links" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"platform" "enum_team_social_links_platform" NOT NULL,
  	"url" varchar NOT NULL
  );
  
  CREATE TABLE "team_qualifications" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"year" numeric,
  	"institution" varchar
  );
  
  CREATE TABLE "team_qualifications_locales" (
  	"title" varchar NOT NULL,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "team_specializations" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL
  );
  
  CREATE TABLE "team_specializations_locales" (
  	"title" varchar NOT NULL,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "team_languages" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"level" "enum_team_languages_level"
  );
  
  CREATE TABLE "team_languages_locales" (
  	"language" varchar NOT NULL,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "team" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"tenant_id" integer,
  	"name" varchar NOT NULL,
  	"image_id" integer,
  	"email" varchar,
  	"phone" varchar,
  	"show_contact_info" boolean DEFAULT false,
  	"linked_user_id" integer,
  	"is_active" boolean DEFAULT true,
  	"is_featured" boolean DEFAULT false,
  	"order" numeric DEFAULT 0,
  	"start_date" 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
  );
  
  CREATE TABLE "team_locales" (
  	"role" varchar NOT NULL,
  	"department" varchar,
  	"bio" jsonb,
  	"bio_short" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" integer NOT NULL
  );
  
  ALTER TABLE "pages_rels" ADD COLUMN "team_id" integer;
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "team_id" integer;
  ALTER TABLE "pages_blocks_team_block" ADD CONSTRAINT "pages_blocks_team_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_team_block_locales" ADD CONSTRAINT "pages_blocks_team_block_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_team_block"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "team_social_links" ADD CONSTRAINT "team_social_links_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."team"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "team_qualifications" ADD CONSTRAINT "team_qualifications_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."team"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "team_qualifications_locales" ADD CONSTRAINT "team_qualifications_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."team_qualifications"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "team_specializations" ADD CONSTRAINT "team_specializations_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."team"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "team_specializations_locales" ADD CONSTRAINT "team_specializations_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."team_specializations"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "team_languages" ADD CONSTRAINT "team_languages_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."team"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "team_languages_locales" ADD CONSTRAINT "team_languages_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."team_languages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "team" ADD CONSTRAINT "team_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "team" ADD CONSTRAINT "team_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "team" ADD CONSTRAINT "team_linked_user_id_users_id_fk" FOREIGN KEY ("linked_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "team_locales" ADD CONSTRAINT "team_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."team"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "pages_blocks_team_block_order_idx" ON "pages_blocks_team_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_team_block_parent_id_idx" ON "pages_blocks_team_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_team_block_path_idx" ON "pages_blocks_team_block" USING btree ("_path");
  CREATE UNIQUE INDEX "pages_blocks_team_block_locales_locale_parent_id_unique" ON "pages_blocks_team_block_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "team_social_links_order_idx" ON "team_social_links" USING btree ("_order");
  CREATE INDEX "team_social_links_parent_id_idx" ON "team_social_links" USING btree ("_parent_id");
  CREATE INDEX "team_qualifications_order_idx" ON "team_qualifications" USING btree ("_order");
  CREATE INDEX "team_qualifications_parent_id_idx" ON "team_qualifications" USING btree ("_parent_id");
  CREATE UNIQUE INDEX "team_qualifications_locales_locale_parent_id_unique" ON "team_qualifications_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "team_specializations_order_idx" ON "team_specializations" USING btree ("_order");
  CREATE INDEX "team_specializations_parent_id_idx" ON "team_specializations" USING btree ("_parent_id");
  CREATE UNIQUE INDEX "team_specializations_locales_locale_parent_id_unique" ON "team_specializations_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "team_languages_order_idx" ON "team_languages" USING btree ("_order");
  CREATE INDEX "team_languages_parent_id_idx" ON "team_languages" USING btree ("_parent_id");
  CREATE UNIQUE INDEX "team_languages_locales_locale_parent_id_unique" ON "team_languages_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "team_tenant_idx" ON "team" USING btree ("tenant_id");
  CREATE INDEX "team_image_idx" ON "team" USING btree ("image_id");
  CREATE INDEX "team_linked_user_idx" ON "team" USING btree ("linked_user_id");
  CREATE INDEX "team_updated_at_idx" ON "team" USING btree ("updated_at");
  CREATE INDEX "team_created_at_idx" ON "team" USING btree ("created_at");
  CREATE UNIQUE INDEX "team_locales_locale_parent_id_unique" ON "team_locales" USING btree ("_locale","_parent_id");
  ALTER TABLE "pages_rels" ADD CONSTRAINT "pages_rels_team_fk" FOREIGN KEY ("team_id") REFERENCES "public"."team"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_team_fk" FOREIGN KEY ("team_id") REFERENCES "public"."team"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "pages_rels_team_id_idx" ON "pages_rels" USING btree ("team_id");
  CREATE INDEX "payload_locked_documents_rels_team_id_idx" ON "payload_locked_documents_rels" USING btree ("team_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "pages_blocks_team_block" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "pages_blocks_team_block_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "team_social_links" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "team_qualifications" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "team_qualifications_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "team_specializations" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "team_specializations_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "team_languages" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "team_languages_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "team" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "team_locales" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "pages_blocks_team_block" CASCADE;
  DROP TABLE "pages_blocks_team_block_locales" CASCADE;
  DROP TABLE "team_social_links" CASCADE;
  DROP TABLE "team_qualifications" CASCADE;
  DROP TABLE "team_qualifications_locales" CASCADE;
  DROP TABLE "team_specializations" CASCADE;
  DROP TABLE "team_specializations_locales" CASCADE;
  DROP TABLE "team_languages" CASCADE;
  DROP TABLE "team_languages_locales" CASCADE;
  DROP TABLE "team" CASCADE;
  DROP TABLE "team_locales" CASCADE;
  ALTER TABLE "pages_rels" DROP CONSTRAINT "pages_rels_team_fk";
  
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_team_fk";
  
  DROP INDEX "pages_rels_team_id_idx";
  DROP INDEX "payload_locked_documents_rels_team_id_idx";
  ALTER TABLE "pages_rels" DROP COLUMN "team_id";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "team_id";
  DROP TYPE "public"."enum_pages_blocks_team_block_display_mode";
  DROP TYPE "public"."enum_pages_blocks_team_block_layout";
  DROP TYPE "public"."enum_pages_blocks_team_block_columns";
  DROP TYPE "public"."enum_pages_blocks_team_block_image_style";
  DROP TYPE "public"."enum_pages_blocks_team_block_background_color";
  DROP TYPE "public"."enum_team_social_links_platform";
  DROP TYPE "public"."enum_team_languages_level";`)
}
