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_hero_block_alignment" AS ENUM('left', 'center', 'right');
  CREATE TYPE "public"."enum_pages_blocks_hero_block_cta_style" AS ENUM('primary', 'secondary', 'outline');
  CREATE TYPE "public"."enum_pages_blocks_text_block_width" AS ENUM('narrow', 'medium', 'full');
  CREATE TYPE "public"."enum_pages_blocks_image_text_block_image_position" AS ENUM('left', 'right');
  CREATE TYPE "public"."enum_pages_blocks_card_grid_block_columns" AS ENUM('2', '3', '4');
  CREATE TYPE "public"."enum_pages_blocks_quote_block_style" AS ENUM('simple', 'highlighted', 'with-image');
  CREATE TYPE "public"."enum_pages_blocks_cta_block_buttons_style" AS ENUM('primary', 'secondary', 'outline');
  CREATE TYPE "public"."enum_pages_blocks_cta_block_background_color" AS ENUM('dark', 'light', 'accent');
  CREATE TYPE "public"."enum_pages_blocks_divider_block_style" AS ENUM('line', 'space', 'dots');
  CREATE TYPE "public"."enum_pages_blocks_divider_block_spacing" AS ENUM('small', 'medium', 'large');
  CREATE TYPE "public"."enum_pages_blocks_video_block_aspect_ratio" AS ENUM('16:9', '4:3', '1:1');
  CREATE TABLE "pages_blocks_hero_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"background_image_id" integer,
  	"headline" varchar NOT NULL,
  	"subline" varchar,
  	"alignment" "enum_pages_blocks_hero_block_alignment" DEFAULT 'center',
  	"overlay" boolean DEFAULT true,
  	"cta_text" varchar,
  	"cta_link" varchar,
  	"cta_style" "enum_pages_blocks_hero_block_cta_style" DEFAULT 'primary',
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_text_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"content" jsonb NOT NULL,
  	"width" "enum_pages_blocks_text_block_width" DEFAULT 'medium',
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_image_text_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"image_id" integer NOT NULL,
  	"image_position" "enum_pages_blocks_image_text_block_image_position" DEFAULT 'left',
  	"headline" varchar,
  	"content" jsonb,
  	"cta_text" varchar,
  	"cta_link" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_card_grid_block_cards" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"image_id" integer,
  	"title" varchar NOT NULL,
  	"description" varchar,
  	"link" varchar,
  	"link_text" varchar DEFAULT 'mehr'
  );
  
  CREATE TABLE "pages_blocks_card_grid_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"headline" varchar,
  	"columns" "enum_pages_blocks_card_grid_block_columns" DEFAULT '3',
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_quote_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"quote" varchar NOT NULL,
  	"author" varchar,
  	"role" varchar,
  	"image_id" integer,
  	"style" "enum_pages_blocks_quote_block_style" DEFAULT 'simple',
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_cta_block_buttons" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"text" varchar NOT NULL,
  	"link" varchar NOT NULL,
  	"style" "enum_pages_blocks_cta_block_buttons_style" DEFAULT 'primary'
  );
  
  CREATE TABLE "pages_blocks_cta_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"headline" varchar NOT NULL,
  	"description" varchar,
  	"background_color" "enum_pages_blocks_cta_block_background_color" DEFAULT 'dark',
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_contact_form_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"headline" varchar DEFAULT 'Kontakt',
  	"description" varchar,
  	"recipient_email" varchar DEFAULT 'info@porwoll.de',
  	"show_phone" boolean DEFAULT true,
  	"show_address" boolean DEFAULT true,
  	"show_socials" boolean DEFAULT true,
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_timeline_block_events" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"year" varchar NOT NULL,
  	"title" varchar NOT NULL,
  	"description" varchar,
  	"image_id" integer
  );
  
  CREATE TABLE "pages_blocks_timeline_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"headline" varchar,
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_divider_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"style" "enum_pages_blocks_divider_block_style" DEFAULT 'space',
  	"spacing" "enum_pages_blocks_divider_block_spacing" DEFAULT 'medium',
  	"block_name" varchar
  );
  
  CREATE TABLE "pages_blocks_video_block" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"video_url" varchar NOT NULL,
  	"caption" varchar,
  	"aspect_ratio" "enum_pages_blocks_video_block_aspect_ratio" DEFAULT '16:9',
  	"block_name" varchar
  );
  
  ALTER TABLE "pages_blocks_hero_block" ADD CONSTRAINT "pages_blocks_hero_block_background_image_id_media_id_fk" FOREIGN KEY ("background_image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "pages_blocks_hero_block" ADD CONSTRAINT "pages_blocks_hero_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_text_block" ADD CONSTRAINT "pages_blocks_text_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_image_text_block" ADD CONSTRAINT "pages_blocks_image_text_block_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "pages_blocks_image_text_block" ADD CONSTRAINT "pages_blocks_image_text_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_card_grid_block_cards" ADD CONSTRAINT "pages_blocks_card_grid_block_cards_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "pages_blocks_card_grid_block_cards" ADD CONSTRAINT "pages_blocks_card_grid_block_cards_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_card_grid_block"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_card_grid_block" ADD CONSTRAINT "pages_blocks_card_grid_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_quote_block" ADD CONSTRAINT "pages_blocks_quote_block_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "pages_blocks_quote_block" ADD CONSTRAINT "pages_blocks_quote_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_cta_block_buttons" ADD CONSTRAINT "pages_blocks_cta_block_buttons_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_cta_block"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_cta_block" ADD CONSTRAINT "pages_blocks_cta_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_contact_form_block" ADD CONSTRAINT "pages_blocks_contact_form_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_timeline_block_events" ADD CONSTRAINT "pages_blocks_timeline_block_events_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "pages_blocks_timeline_block_events" ADD CONSTRAINT "pages_blocks_timeline_block_events_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages_blocks_timeline_block"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_timeline_block" ADD CONSTRAINT "pages_blocks_timeline_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_divider_block" ADD CONSTRAINT "pages_blocks_divider_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "pages_blocks_video_block" ADD CONSTRAINT "pages_blocks_video_block_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "pages_blocks_hero_block_order_idx" ON "pages_blocks_hero_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_hero_block_parent_id_idx" ON "pages_blocks_hero_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_hero_block_path_idx" ON "pages_blocks_hero_block" USING btree ("_path");
  CREATE INDEX "pages_blocks_hero_block_background_image_idx" ON "pages_blocks_hero_block" USING btree ("background_image_id");
  CREATE INDEX "pages_blocks_text_block_order_idx" ON "pages_blocks_text_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_text_block_parent_id_idx" ON "pages_blocks_text_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_text_block_path_idx" ON "pages_blocks_text_block" USING btree ("_path");
  CREATE INDEX "pages_blocks_image_text_block_order_idx" ON "pages_blocks_image_text_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_image_text_block_parent_id_idx" ON "pages_blocks_image_text_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_image_text_block_path_idx" ON "pages_blocks_image_text_block" USING btree ("_path");
  CREATE INDEX "pages_blocks_image_text_block_image_idx" ON "pages_blocks_image_text_block" USING btree ("image_id");
  CREATE INDEX "pages_blocks_card_grid_block_cards_order_idx" ON "pages_blocks_card_grid_block_cards" USING btree ("_order");
  CREATE INDEX "pages_blocks_card_grid_block_cards_parent_id_idx" ON "pages_blocks_card_grid_block_cards" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_card_grid_block_cards_image_idx" ON "pages_blocks_card_grid_block_cards" USING btree ("image_id");
  CREATE INDEX "pages_blocks_card_grid_block_order_idx" ON "pages_blocks_card_grid_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_card_grid_block_parent_id_idx" ON "pages_blocks_card_grid_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_card_grid_block_path_idx" ON "pages_blocks_card_grid_block" USING btree ("_path");
  CREATE INDEX "pages_blocks_quote_block_order_idx" ON "pages_blocks_quote_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_quote_block_parent_id_idx" ON "pages_blocks_quote_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_quote_block_path_idx" ON "pages_blocks_quote_block" USING btree ("_path");
  CREATE INDEX "pages_blocks_quote_block_image_idx" ON "pages_blocks_quote_block" USING btree ("image_id");
  CREATE INDEX "pages_blocks_cta_block_buttons_order_idx" ON "pages_blocks_cta_block_buttons" USING btree ("_order");
  CREATE INDEX "pages_blocks_cta_block_buttons_parent_id_idx" ON "pages_blocks_cta_block_buttons" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_cta_block_order_idx" ON "pages_blocks_cta_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_cta_block_parent_id_idx" ON "pages_blocks_cta_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_cta_block_path_idx" ON "pages_blocks_cta_block" USING btree ("_path");
  CREATE INDEX "pages_blocks_contact_form_block_order_idx" ON "pages_blocks_contact_form_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_contact_form_block_parent_id_idx" ON "pages_blocks_contact_form_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_contact_form_block_path_idx" ON "pages_blocks_contact_form_block" USING btree ("_path");
  CREATE INDEX "pages_blocks_timeline_block_events_order_idx" ON "pages_blocks_timeline_block_events" USING btree ("_order");
  CREATE INDEX "pages_blocks_timeline_block_events_parent_id_idx" ON "pages_blocks_timeline_block_events" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_timeline_block_events_image_idx" ON "pages_blocks_timeline_block_events" USING btree ("image_id");
  CREATE INDEX "pages_blocks_timeline_block_order_idx" ON "pages_blocks_timeline_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_timeline_block_parent_id_idx" ON "pages_blocks_timeline_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_timeline_block_path_idx" ON "pages_blocks_timeline_block" USING btree ("_path");
  CREATE INDEX "pages_blocks_divider_block_order_idx" ON "pages_blocks_divider_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_divider_block_parent_id_idx" ON "pages_blocks_divider_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_divider_block_path_idx" ON "pages_blocks_divider_block" USING btree ("_path");
  CREATE INDEX "pages_blocks_video_block_order_idx" ON "pages_blocks_video_block" USING btree ("_order");
  CREATE INDEX "pages_blocks_video_block_parent_id_idx" ON "pages_blocks_video_block" USING btree ("_parent_id");
  CREATE INDEX "pages_blocks_video_block_path_idx" ON "pages_blocks_video_block" USING btree ("_path");
  ALTER TABLE "pages" DROP COLUMN "content";`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   DROP TABLE "pages_blocks_hero_block" CASCADE;
  DROP TABLE "pages_blocks_text_block" CASCADE;
  DROP TABLE "pages_blocks_image_text_block" CASCADE;
  DROP TABLE "pages_blocks_card_grid_block_cards" CASCADE;
  DROP TABLE "pages_blocks_card_grid_block" CASCADE;
  DROP TABLE "pages_blocks_quote_block" CASCADE;
  DROP TABLE "pages_blocks_cta_block_buttons" CASCADE;
  DROP TABLE "pages_blocks_cta_block" CASCADE;
  DROP TABLE "pages_blocks_contact_form_block" CASCADE;
  DROP TABLE "pages_blocks_timeline_block_events" CASCADE;
  DROP TABLE "pages_blocks_timeline_block" CASCADE;
  DROP TABLE "pages_blocks_divider_block" CASCADE;
  DROP TABLE "pages_blocks_video_block" CASCADE;
  ALTER TABLE "pages" ADD COLUMN "content" jsonb;
  DROP TYPE "public"."enum_pages_blocks_hero_block_alignment";
  DROP TYPE "public"."enum_pages_blocks_hero_block_cta_style";
  DROP TYPE "public"."enum_pages_blocks_text_block_width";
  DROP TYPE "public"."enum_pages_blocks_image_text_block_image_position";
  DROP TYPE "public"."enum_pages_blocks_card_grid_block_columns";
  DROP TYPE "public"."enum_pages_blocks_quote_block_style";
  DROP TYPE "public"."enum_pages_blocks_cta_block_buttons_style";
  DROP TYPE "public"."enum_pages_blocks_cta_block_background_color";
  DROP TYPE "public"."enum_pages_blocks_divider_block_style";
  DROP TYPE "public"."enum_pages_blocks_divider_block_spacing";
  DROP TYPE "public"."enum_pages_blocks_video_block_aspect_ratio";`)
}
