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_services_block_display_mode" AS ENUM('all', 'featured', 'category', 'selected');
  CREATE TYPE "public"."enum_pages_blocks_services_block_layout" AS ENUM('grid', 'list', 'tabs', 'accordion', 'featured-grid', 'slider', 'compact', 'masonry');
  CREATE TYPE "public"."enum_pages_blocks_services_block_columns" AS ENUM('2', '3', '4');
  CREATE TYPE "public"."enum_pages_blocks_services_block_tabs_style" AS ENUM('horizontal', 'vertical', 'pills');
  CREATE TYPE "public"."enum_pages_blocks_services_block_slides_per_view" AS ENUM('1', '2', '3', '4');
  CREATE TYPE "public"."enum_pages_blocks_services_block_card_style" AS ENUM('elevated', 'bordered', 'flat', 'filled');
  CREATE TYPE "public"."enum_pages_blocks_services_block_background_color" AS ENUM('white', 'light', 'dark', 'accent');
  CREATE TYPE "public"."enum_services_pricing_type" AS ENUM('on-request', 'fixed', 'from', 'range', 'hourly', 'monthly', 'free');
  CREATE TYPE "public"."enum_services_cta_style" AS ENUM('primary', 'secondary', 'outline');
  CREATE TABLE "pages_blocks_services_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_services_block_display_mode" DEFAULT 'all',
  	"category_id" integer,
  	"limit" numeric DEFAULT 12,
  	"layout" "enum_pages_blocks_services_block_layout" DEFAULT 'grid',
  	"columns" "enum_pages_blocks_services_block_columns" DEFAULT '3',
  	"featured_count" numeric DEFAULT 1,
  	"tabs_style" "enum_pages_blocks_services_block_tabs_style" DEFAULT 'horizontal',
  	"show_all_tab" boolean DEFAULT true,
  	"expand_first" boolean DEFAULT true,
  	"allow_multiple_open" boolean DEFAULT false,
  	"autoplay" boolean DEFAULT true,
  	"autoplay_speed" numeric DEFAULT 5000,
  	"slides_per_view" "enum_pages_blocks_services_block_slides_per_view" DEFAULT '3',
  	"show_image" boolean DEFAULT true,
  	"show_icon" boolean DEFAULT true,
  	"show_description" boolean DEFAULT true,
  	"show_category" boolean DEFAULT false,
  	"show_pricing" boolean DEFAULT true,
  	"show_features" boolean DEFAULT false,
  	"features_limit" numeric DEFAULT 3,
  	"show_c_t_a" boolean DEFAULT true,
  	"show_new_badge" boolean DEFAULT true,
  	"group_by_category" boolean DEFAULT false,
  	"link_to_detail" boolean DEFAULT true,
  	"services_base_path" varchar DEFAULT '/leistungen',
  	"card_style" "enum_pages_blocks_services_block_card_style" DEFAULT 'elevated',
  	"background_color" "enum_pages_blocks_services_block_background_color" DEFAULT 'white',
  	"show_section_c_t_a" boolean DEFAULT false,
  	"section_c_t_a_link" varchar DEFAULT '/leistungen',
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_services_block_locales" (
  	"title" varchar DEFAULT 'Unsere Leistungen',
  	"subtitle" varchar,
  	"introduction" jsonb,
  	"detail_link_text" varchar DEFAULT 'Mehr erfahren',
  	"section_c_t_a_text" varchar DEFAULT 'Alle Leistungen ansehen',
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "service_categories" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"tenant_id" integer,
  	"slug" varchar NOT NULL,
  	"icon" varchar,
  	"image_id" integer,
  	"color" varchar,
  	"is_active" boolean DEFAULT true,
  	"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 "service_categories_locales" (
  	"name" varchar NOT NULL,
  	"description" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" integer NOT NULL
  );
  
  CREATE TABLE "services_gallery" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"image_id" integer NOT NULL
  );
  
  CREATE TABLE "services_gallery_locales" (
  	"caption" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "services_features" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"icon" varchar
  );
  
  CREATE TABLE "services_features_locales" (
  	"title" varchar NOT NULL,
  	"description" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "services_detail_sections" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"icon" varchar
  );
  
  CREATE TABLE "services_detail_sections_locales" (
  	"title" varchar NOT NULL,
  	"content" jsonb NOT NULL,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "services" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"tenant_id" integer,
  	"slug" varchar NOT NULL,
  	"icon" varchar,
  	"icon_image_id" integer,
  	"image_id" integer,
  	"category_id" integer,
  	"pricing_type" "enum_services_pricing_type" DEFAULT 'on-request',
  	"price" numeric,
  	"price_max" numeric,
  	"cta_link" varchar DEFAULT '/kontakt',
  	"cta_style" "enum_services_cta_style" DEFAULT 'primary',
  	"secondary_cta_enabled" boolean DEFAULT false,
  	"secondary_cta_link" varchar,
  	"testimonial_author" varchar,
  	"og_image_id" integer,
  	"is_active" boolean DEFAULT true,
  	"is_featured" boolean DEFAULT false,
  	"is_new" 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 "services_locales" (
  	"title" varchar NOT NULL,
  	"subtitle" varchar,
  	"short_description" varchar NOT NULL,
  	"description" jsonb,
  	"price_unit" varchar,
  	"price_note" varchar,
  	"pricing_details" jsonb,
  	"cta_text" varchar DEFAULT 'Jetzt anfragen',
  	"secondary_cta_text" varchar,
  	"testimonial_quote" varchar,
  	"meta_title" varchar,
  	"meta_description" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" integer NOT NULL
  );
  
  CREATE TABLE "services_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" integer NOT NULL,
  	"path" varchar NOT NULL,
  	"services_id" integer,
  	"team_id" integer,
  	"faqs_id" integer
  );
  
  ALTER TABLE "pages_rels" ADD COLUMN "services_id" integer;
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "service_categories_id" integer;
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "services_id" integer;
  ALTER TABLE "pages_blocks_services_block" ADD CONSTRAINT "pages_blocks_services_block_category_id_service_categories_id_fk" FOREIGN KEY ("category_id") REFERENCES "public"."service_categories"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "pages_blocks_services_block" ADD CONSTRAINT "pages_blocks_services_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_services_block_locales" ADD CONSTRAINT "pages_blocks_services_block_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_services_block"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "service_categories" ADD CONSTRAINT "service_categories_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "service_categories" ADD CONSTRAINT "service_categories_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "service_categories_locales" ADD CONSTRAINT "service_categories_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."service_categories"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "services_gallery" ADD CONSTRAINT "services_gallery_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "services_gallery" ADD CONSTRAINT "services_gallery_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."services"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "services_gallery_locales" ADD CONSTRAINT "services_gallery_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."services_gallery"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "services_features" ADD CONSTRAINT "services_features_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."services"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "services_features_locales" ADD CONSTRAINT "services_features_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."services_features"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "services_detail_sections" ADD CONSTRAINT "services_detail_sections_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."services"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "services_detail_sections_locales" ADD CONSTRAINT "services_detail_sections_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."services_detail_sections"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "services" ADD CONSTRAINT "services_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "services" ADD CONSTRAINT "services_icon_image_id_media_id_fk" FOREIGN KEY ("icon_image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "services" ADD CONSTRAINT "services_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "services" ADD CONSTRAINT "services_category_id_service_categories_id_fk" FOREIGN KEY ("category_id") REFERENCES "public"."service_categories"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "services" ADD CONSTRAINT "services_og_image_id_media_id_fk" FOREIGN KEY ("og_image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "services_locales" ADD CONSTRAINT "services_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."services"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "services_rels" ADD CONSTRAINT "services_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."services"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "services_rels" ADD CONSTRAINT "services_rels_services_fk" FOREIGN KEY ("services_id") REFERENCES "public"."services"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "services_rels" ADD CONSTRAINT "services_rels_team_fk" FOREIGN KEY ("team_id") REFERENCES "public"."team"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "services_rels" ADD CONSTRAINT "services_rels_faqs_fk" FOREIGN KEY ("faqs_id") REFERENCES "public"."faqs"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "pages_blocks_services_block_order_idx" ON "pages_blocks_services_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_services_block_parent_id_idx" ON "pages_blocks_services_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_services_block_path_idx" ON "pages_blocks_services_block" USING btree ("_path");
  CREATE INDEX "pages_blocks_services_block_category_idx" ON "pages_blocks_services_block" USING btree ("category_id");
  CREATE UNIQUE INDEX "pages_blocks_services_block_locales_locale_parent_id_unique" ON "pages_blocks_services_block_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "service_categories_tenant_idx" ON "service_categories" USING btree ("tenant_id");
  CREATE INDEX "service_categories_image_idx" ON "service_categories" USING btree ("image_id");
  CREATE INDEX "service_categories_updated_at_idx" ON "service_categories" USING btree ("updated_at");
  CREATE INDEX "service_categories_created_at_idx" ON "service_categories" USING btree ("created_at");
  CREATE UNIQUE INDEX "service_categories_locales_locale_parent_id_unique" ON "service_categories_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "services_gallery_order_idx" ON "services_gallery" USING btree ("_order");
  CREATE INDEX "services_gallery_parent_id_idx" ON "services_gallery" USING btree ("_parent_id");
  CREATE INDEX "services_gallery_image_idx" ON "services_gallery" USING btree ("image_id");
  CREATE UNIQUE INDEX "services_gallery_locales_locale_parent_id_unique" ON "services_gallery_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "services_features_order_idx" ON "services_features" USING btree ("_order");
  CREATE INDEX "services_features_parent_id_idx" ON "services_features" USING btree ("_parent_id");
  CREATE UNIQUE INDEX "services_features_locales_locale_parent_id_unique" ON "services_features_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "services_detail_sections_order_idx" ON "services_detail_sections" USING btree ("_order");
  CREATE INDEX "services_detail_sections_parent_id_idx" ON "services_detail_sections" USING btree ("_parent_id");
  CREATE UNIQUE INDEX "services_detail_sections_locales_locale_parent_id_unique" ON "services_detail_sections_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "services_tenant_idx" ON "services" USING btree ("tenant_id");
  CREATE INDEX "services_icon_image_idx" ON "services" USING btree ("icon_image_id");
  CREATE INDEX "services_image_idx" ON "services" USING btree ("image_id");
  CREATE INDEX "services_category_idx" ON "services" USING btree ("category_id");
  CREATE INDEX "services_og_image_idx" ON "services" USING btree ("og_image_id");
  CREATE INDEX "services_updated_at_idx" ON "services" USING btree ("updated_at");
  CREATE INDEX "services_created_at_idx" ON "services" USING btree ("created_at");
  CREATE UNIQUE INDEX "services_locales_locale_parent_id_unique" ON "services_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "services_rels_order_idx" ON "services_rels" USING btree ("order");
  CREATE INDEX "services_rels_parent_idx" ON "services_rels" USING btree ("parent_id");
  CREATE INDEX "services_rels_path_idx" ON "services_rels" USING btree ("path");
  CREATE INDEX "services_rels_services_id_idx" ON "services_rels" USING btree ("services_id");
  CREATE INDEX "services_rels_team_id_idx" ON "services_rels" USING btree ("team_id");
  CREATE INDEX "services_rels_faqs_id_idx" ON "services_rels" USING btree ("faqs_id");
  ALTER TABLE "pages_rels" ADD CONSTRAINT "pages_rels_services_fk" FOREIGN KEY ("services_id") REFERENCES "public"."services"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_service_categories_fk" FOREIGN KEY ("service_categories_id") REFERENCES "public"."service_categories"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_services_fk" FOREIGN KEY ("services_id") REFERENCES "public"."services"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "pages_rels_services_id_idx" ON "pages_rels" USING btree ("services_id");
  CREATE INDEX "payload_locked_documents_rels_service_categories_id_idx" ON "payload_locked_documents_rels" USING btree ("service_categories_id");
  CREATE INDEX "payload_locked_documents_rels_services_id_idx" ON "payload_locked_documents_rels" USING btree ("services_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "pages_blocks_services_block" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "pages_blocks_services_block_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "service_categories" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "service_categories_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "services_gallery" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "services_gallery_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "services_features" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "services_features_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "services_detail_sections" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "services_detail_sections_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "services" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "services_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "services_rels" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "pages_blocks_services_block" CASCADE;
  DROP TABLE "pages_blocks_services_block_locales" CASCADE;
  DROP TABLE "service_categories" CASCADE;
  DROP TABLE "service_categories_locales" CASCADE;
  DROP TABLE "services_gallery" CASCADE;
  DROP TABLE "services_gallery_locales" CASCADE;
  DROP TABLE "services_features" CASCADE;
  DROP TABLE "services_features_locales" CASCADE;
  DROP TABLE "services_detail_sections" CASCADE;
  DROP TABLE "services_detail_sections_locales" CASCADE;
  DROP TABLE "services" CASCADE;
  DROP TABLE "services_locales" CASCADE;
  DROP TABLE "services_rels" CASCADE;
  ALTER TABLE "pages_rels" DROP CONSTRAINT "pages_rels_services_fk";
  
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_service_categories_fk";
  
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_services_fk";
  
  DROP INDEX "pages_rels_services_id_idx";
  DROP INDEX "payload_locked_documents_rels_service_categories_id_idx";
  DROP INDEX "payload_locked_documents_rels_services_id_idx";
  ALTER TABLE "pages_rels" DROP COLUMN "services_id";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "service_categories_id";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "services_id";
  DROP TYPE "public"."enum_pages_blocks_services_block_display_mode";
  DROP TYPE "public"."enum_pages_blocks_services_block_layout";
  DROP TYPE "public"."enum_pages_blocks_services_block_columns";
  DROP TYPE "public"."enum_pages_blocks_services_block_tabs_style";
  DROP TYPE "public"."enum_pages_blocks_services_block_slides_per_view";
  DROP TYPE "public"."enum_pages_blocks_services_block_card_style";
  DROP TYPE "public"."enum_pages_blocks_services_block_background_color";
  DROP TYPE "public"."enum_services_pricing_type";
  DROP TYPE "public"."enum_services_cta_style";`)
}
