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_faq_block_source_mode" AS ENUM('collection', 'inline');
  CREATE TYPE "public"."enum_pages_blocks_faq_block_display_mode" AS ENUM('all', 'featured', 'category', 'selected');
  CREATE TYPE "public"."enum_pages_blocks_faq_block_layout" AS ENUM('accordion', 'grid', 'list', 'two-column');
  CREATE TYPE "public"."enum_pages_blocks_faq_block_columns" AS ENUM('2', '3');
  CREATE TYPE "public"."enum_pages_blocks_faq_block_background_color" AS ENUM('white', 'light', 'dark', 'accent');
  CREATE TABLE "pages_blocks_faq_block_inline_f_a_qs" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL
  );
  
  CREATE TABLE "pages_blocks_faq_block_inline_f_a_qs_locales" (
  	"question" varchar,
  	"answer" jsonb,
  	"answer_plain_text" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "pages_blocks_faq_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"source_mode" "enum_pages_blocks_faq_block_source_mode" DEFAULT 'collection',
  	"display_mode" "enum_pages_blocks_faq_block_display_mode" DEFAULT 'all',
  	"category" varchar,
  	"limit" numeric DEFAULT 10,
  	"layout" "enum_pages_blocks_faq_block_layout" DEFAULT 'accordion',
  	"columns" "enum_pages_blocks_faq_block_columns" DEFAULT '2',
  	"expand_first" boolean DEFAULT true,
  	"allow_multiple_open" boolean DEFAULT false,
  	"show_category" boolean DEFAULT false,
  	"show_icon" boolean DEFAULT false,
  	"group_by_category" boolean DEFAULT false,
  	"enable_schema_org" boolean DEFAULT true,
  	"background_color" "enum_pages_blocks_faq_block_background_color" DEFAULT 'white',
  	"show_contact_c_t_a" boolean DEFAULT false,
  	"contact_c_t_a_link" varchar DEFAULT '/kontakt',
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_faq_block_locales" (
  	"title" varchar DEFAULT 'Häufig gestellte Fragen',
  	"subtitle" varchar,
  	"contact_c_t_a_text" varchar DEFAULT 'Ihre Frage war nicht dabei?',
  	"contact_c_t_a_button_text" varchar DEFAULT 'Kontaktieren Sie uns',
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "faqs" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"tenant_id" integer,
  	"icon" varchar,
  	"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
  );
  
  CREATE TABLE "faqs_locales" (
  	"question" varchar NOT NULL,
  	"answer" jsonb NOT NULL,
  	"answer_plain_text" varchar,
  	"category" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" integer NOT NULL
  );
  
  CREATE TABLE "faqs_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" integer NOT NULL,
  	"path" varchar NOT NULL,
  	"faqs_id" integer
  );
  
  ALTER TABLE "pages_rels" ADD COLUMN "faqs_id" integer;
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "faqs_id" integer;
  ALTER TABLE "pages_blocks_faq_block_inline_f_a_qs" ADD CONSTRAINT "pages_blocks_faq_block_inline_f_a_qs_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_faq_block"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_faq_block_inline_f_a_qs_locales" ADD CONSTRAINT "pages_blocks_faq_block_inline_f_a_qs_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_faq_block_inline_f_a_qs"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_faq_block" ADD CONSTRAINT "pages_blocks_faq_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_faq_block_locales" ADD CONSTRAINT "pages_blocks_faq_block_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_faq_block"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "faqs" ADD CONSTRAINT "faqs_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "faqs_locales" ADD CONSTRAINT "faqs_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."faqs"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "faqs_rels" ADD CONSTRAINT "faqs_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."faqs"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "faqs_rels" ADD CONSTRAINT "faqs_rels_faqs_fk" FOREIGN KEY ("faqs_id") REFERENCES "public"."faqs"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "pages_blocks_faq_block_inline_f_a_qs_order_idx" ON "pages_blocks_faq_block_inline_f_a_qs" USING btree ("_order");
  CREATE INDEX "pages_blocks_faq_block_inline_f_a_qs_parent_id_idx" ON "pages_blocks_faq_block_inline_f_a_qs" USING btree ("_parent_id");
  CREATE UNIQUE INDEX "pages_blocks_faq_block_inline_f_a_qs_locales_locale_parent_i" ON "pages_blocks_faq_block_inline_f_a_qs_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "pages_blocks_faq_block_order_idx" ON "pages_blocks_faq_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_faq_block_parent_id_idx" ON "pages_blocks_faq_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_faq_block_path_idx" ON "pages_blocks_faq_block" USING btree ("_path");
  CREATE UNIQUE INDEX "pages_blocks_faq_block_locales_locale_parent_id_unique" ON "pages_blocks_faq_block_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "faqs_tenant_idx" ON "faqs" USING btree ("tenant_id");
  CREATE INDEX "faqs_updated_at_idx" ON "faqs" USING btree ("updated_at");
  CREATE INDEX "faqs_created_at_idx" ON "faqs" USING btree ("created_at");
  CREATE UNIQUE INDEX "faqs_locales_locale_parent_id_unique" ON "faqs_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "faqs_rels_order_idx" ON "faqs_rels" USING btree ("order");
  CREATE INDEX "faqs_rels_parent_idx" ON "faqs_rels" USING btree ("parent_id");
  CREATE INDEX "faqs_rels_path_idx" ON "faqs_rels" USING btree ("path");
  CREATE INDEX "faqs_rels_faqs_id_idx" ON "faqs_rels" USING btree ("faqs_id");
  ALTER TABLE "pages_rels" ADD CONSTRAINT "pages_rels_faqs_fk" FOREIGN KEY ("faqs_id") REFERENCES "public"."faqs"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_faqs_fk" FOREIGN KEY ("faqs_id") REFERENCES "public"."faqs"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "pages_rels_faqs_id_idx" ON "pages_rels" USING btree ("faqs_id");
  CREATE INDEX "payload_locked_documents_rels_faqs_id_idx" ON "payload_locked_documents_rels" USING btree ("faqs_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "pages_blocks_faq_block_inline_f_a_qs" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "pages_blocks_faq_block_inline_f_a_qs_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "pages_blocks_faq_block" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "pages_blocks_faq_block_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "faqs" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "faqs_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "faqs_rels" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "pages_blocks_faq_block_inline_f_a_qs" CASCADE;
  DROP TABLE "pages_blocks_faq_block_inline_f_a_qs_locales" CASCADE;
  DROP TABLE "pages_blocks_faq_block" CASCADE;
  DROP TABLE "pages_blocks_faq_block_locales" CASCADE;
  DROP TABLE "faqs" CASCADE;
  DROP TABLE "faqs_locales" CASCADE;
  DROP TABLE "faqs_rels" CASCADE;
  ALTER TABLE "pages_rels" DROP CONSTRAINT "pages_rels_faqs_fk";
  
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_faqs_fk";
  
  DROP INDEX "pages_rels_faqs_id_idx";
  DROP INDEX "payload_locked_documents_rels_faqs_id_idx";
  ALTER TABLE "pages_rels" DROP COLUMN "faqs_id";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "faqs_id";
  DROP TYPE "public"."enum_pages_blocks_faq_block_source_mode";
  DROP TYPE "public"."enum_pages_blocks_faq_block_display_mode";
  DROP TYPE "public"."enum_pages_blocks_faq_block_layout";
  DROP TYPE "public"."enum_pages_blocks_faq_block_columns";
  DROP TYPE "public"."enum_pages_blocks_faq_block_background_color";`)
}
