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_timelines_events_links_type" AS ENUM('internal', 'external', 'download');
  CREATE TYPE "public"."enum_timelines_events_date_type" AS ENUM('year', 'monthYear', 'fullDate', 'range', 'custom');
  CREATE TYPE "public"."enum_timelines_events_month" AS ENUM('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12');
  CREATE TYPE "public"."enum_timelines_events_end_month" AS ENUM('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12');
  CREATE TYPE "public"."enum_timelines_events_category" AS ENUM('milestone', 'founding', 'product', 'team', 'award', 'partnership', 'expansion', 'technology', 'other');
  CREATE TYPE "public"."enum_timelines_events_importance" AS ENUM('highlight', 'normal', 'minor');
  CREATE TYPE "public"."enum_timelines_type" AS ENUM('history', 'milestones', 'releases', 'career', 'events', 'process');
  CREATE TYPE "public"."enum_timelines_status" AS ENUM('draft', 'published', 'archived');
  CREATE TYPE "public"."enum_timelines_display_options_layout" AS ENUM('vertical', 'alternating', 'horizontal', 'compact');
  CREATE TYPE "public"."enum_timelines_display_options_sort_order" AS ENUM('desc', 'asc');
  CREATE TYPE "public"."enum_timelines_display_options_marker_style" AS ENUM('dot', 'number', 'icon', 'date');
  CREATE TYPE "public"."enum_timelines_display_options_color_scheme" AS ENUM('primary', 'secondary', 'neutral', 'colorful');
  CREATE TABLE "timelines_events_gallery" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"image_id" integer NOT NULL
  );
  
  CREATE TABLE "timelines_events_gallery_locales" (
  	"caption" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "timelines_events_links" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"url" varchar NOT NULL,
  	"type" "enum_timelines_events_links_type" DEFAULT 'internal'
  );
  
  CREATE TABLE "timelines_events_links_locales" (
  	"label" varchar NOT NULL,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "timelines_events" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"date_type" "enum_timelines_events_date_type" DEFAULT 'year',
  	"year" numeric,
  	"month" "enum_timelines_events_month",
  	"day" numeric,
  	"end_year" numeric,
  	"end_month" "enum_timelines_events_end_month",
  	"ongoing" boolean,
  	"custom_date" varchar,
  	"image_id" integer,
  	"category" "enum_timelines_events_category",
  	"importance" "enum_timelines_events_importance" DEFAULT 'normal',
  	"icon" varchar,
  	"color" varchar,
  	"metadata" jsonb
  );
  
  CREATE TABLE "timelines_events_locales" (
  	"title" varchar NOT NULL,
  	"subtitle" varchar,
  	"description" jsonb,
  	"short_description" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "timelines" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"tenant_id" integer,
  	"slug" varchar NOT NULL,
  	"type" "enum_timelines_type" DEFAULT 'history' NOT NULL,
  	"status" "enum_timelines_status" DEFAULT 'draft' NOT NULL,
  	"display_options_layout" "enum_timelines_display_options_layout" DEFAULT 'vertical',
  	"display_options_sort_order" "enum_timelines_display_options_sort_order" DEFAULT 'desc',
  	"display_options_show_connector" boolean DEFAULT true,
  	"display_options_show_images" boolean DEFAULT true,
  	"display_options_group_by_year" boolean DEFAULT false,
  	"display_options_marker_style" "enum_timelines_display_options_marker_style" DEFAULT 'dot',
  	"display_options_color_scheme" "enum_timelines_display_options_color_scheme" DEFAULT 'primary',
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
  );
  
  CREATE TABLE "timelines_locales" (
  	"name" varchar NOT NULL,
  	"description" varchar,
  	"seo_meta_title" varchar,
  	"seo_meta_description" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" integer NOT NULL
  );
  
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "timelines_id" integer;
  ALTER TABLE "timelines_events_gallery" ADD CONSTRAINT "timelines_events_gallery_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "timelines_events_gallery" ADD CONSTRAINT "timelines_events_gallery_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."timelines_events"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "timelines_events_gallery_locales" ADD CONSTRAINT "timelines_events_gallery_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."timelines_events_gallery"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "timelines_events_links" ADD CONSTRAINT "timelines_events_links_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."timelines_events"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "timelines_events_links_locales" ADD CONSTRAINT "timelines_events_links_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."timelines_events_links"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "timelines_events" ADD CONSTRAINT "timelines_events_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "timelines_events" ADD CONSTRAINT "timelines_events_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."timelines"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "timelines_events_locales" ADD CONSTRAINT "timelines_events_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."timelines_events"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "timelines" ADD CONSTRAINT "timelines_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "timelines_locales" ADD CONSTRAINT "timelines_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."timelines"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "timelines_events_gallery_order_idx" ON "timelines_events_gallery" USING btree ("_order");
  CREATE INDEX "timelines_events_gallery_parent_id_idx" ON "timelines_events_gallery" USING btree ("_parent_id");
  CREATE INDEX "timelines_events_gallery_image_idx" ON "timelines_events_gallery" USING btree ("image_id");
  CREATE UNIQUE INDEX "timelines_events_gallery_locales_locale_parent_id_unique" ON "timelines_events_gallery_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "timelines_events_links_order_idx" ON "timelines_events_links" USING btree ("_order");
  CREATE INDEX "timelines_events_links_parent_id_idx" ON "timelines_events_links" USING btree ("_parent_id");
  CREATE UNIQUE INDEX "timelines_events_links_locales_locale_parent_id_unique" ON "timelines_events_links_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "timelines_events_order_idx" ON "timelines_events" USING btree ("_order");
  CREATE INDEX "timelines_events_parent_id_idx" ON "timelines_events" USING btree ("_parent_id");
  CREATE INDEX "timelines_events_image_idx" ON "timelines_events" USING btree ("image_id");
  CREATE UNIQUE INDEX "timelines_events_locales_locale_parent_id_unique" ON "timelines_events_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "timelines_tenant_idx" ON "timelines" USING btree ("tenant_id");
  CREATE UNIQUE INDEX "timelines_slug_idx" ON "timelines" USING btree ("slug");
  CREATE INDEX "timelines_updated_at_idx" ON "timelines" USING btree ("updated_at");
  CREATE INDEX "timelines_created_at_idx" ON "timelines" USING btree ("created_at");
  CREATE UNIQUE INDEX "timelines_locales_locale_parent_id_unique" ON "timelines_locales" USING btree ("_locale","_parent_id");
  ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_timelines_fk" FOREIGN KEY ("timelines_id") REFERENCES "public"."timelines"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "payload_locked_documents_rels_timelines_id_idx" ON "payload_locked_documents_rels" USING btree ("timelines_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "timelines_events_gallery" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "timelines_events_gallery_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "timelines_events_links" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "timelines_events_links_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "timelines_events" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "timelines_events_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "timelines" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "timelines_locales" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "timelines_events_gallery" CASCADE;
  DROP TABLE "timelines_events_gallery_locales" CASCADE;
  DROP TABLE "timelines_events_links" CASCADE;
  DROP TABLE "timelines_events_links_locales" CASCADE;
  DROP TABLE "timelines_events" CASCADE;
  DROP TABLE "timelines_events_locales" CASCADE;
  DROP TABLE "timelines" CASCADE;
  DROP TABLE "timelines_locales" CASCADE;
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_timelines_fk";
  
  DROP INDEX "payload_locked_documents_rels_timelines_id_idx";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "timelines_id";
  DROP TYPE "public"."enum_timelines_events_links_type";
  DROP TYPE "public"."enum_timelines_events_date_type";
  DROP TYPE "public"."enum_timelines_events_month";
  DROP TYPE "public"."enum_timelines_events_end_month";
  DROP TYPE "public"."enum_timelines_events_category";
  DROP TYPE "public"."enum_timelines_events_importance";
  DROP TYPE "public"."enum_timelines_type";
  DROP TYPE "public"."enum_timelines_status";
  DROP TYPE "public"."enum_timelines_display_options_layout";
  DROP TYPE "public"."enum_timelines_display_options_sort_order";
  DROP TYPE "public"."enum_timelines_display_options_marker_style";
  DROP TYPE "public"."enum_timelines_display_options_color_scheme";`)
}
