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_portfolios_status" AS ENUM('draft', 'published', 'archived');
  CREATE TABLE "portfolio_categories" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"tenant_id" integer,
  	"slug" varchar NOT NULL,
  	"cover_image_id" integer,
  	"order" numeric DEFAULT 0,
  	"is_active" boolean DEFAULT true,
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
  );
  
  CREATE TABLE "portfolio_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 "portfolios_images" (
  	"_order" integer NOT NULL,
  	"_parent_id" integer NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"image_id" integer NOT NULL,
  	"is_highlight" boolean DEFAULT false
  );
  
  CREATE TABLE "portfolios_images_locales" (
  	"caption" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" varchar NOT NULL
  );
  
  CREATE TABLE "portfolios" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"tenant_id" integer,
  	"slug" varchar NOT NULL,
  	"category_id" integer NOT NULL,
  	"cover_image_id" integer NOT NULL,
  	"project_details_client" varchar,
  	"project_details_shooting_date" timestamp(3) with time zone,
  	"status" "enum_portfolios_status" DEFAULT 'draft' NOT NULL,
  	"is_featured" boolean DEFAULT false,
  	"published_at" timestamp(3) with time zone,
  	"order" numeric DEFAULT 0,
  	"seo_og_image_id" integer,
  	"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
  	"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
  );
  
  CREATE TABLE "portfolios_locales" (
  	"title" varchar NOT NULL,
  	"description" jsonb,
  	"excerpt" varchar,
  	"project_details_location" varchar,
  	"seo_meta_title" varchar,
  	"seo_meta_description" varchar,
  	"id" serial PRIMARY KEY NOT NULL,
  	"_locale" "_locales" NOT NULL,
  	"_parent_id" integer NOT NULL
  );
  
  CREATE TABLE "portfolios_texts" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer NOT NULL,
  	"parent_id" integer NOT NULL,
  	"path" varchar NOT NULL,
  	"text" varchar
  );
  
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "portfolio_categories_id" integer;
  ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "portfolios_id" integer;
  ALTER TABLE "portfolio_categories" ADD CONSTRAINT "portfolio_categories_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "portfolio_categories" ADD CONSTRAINT "portfolio_categories_cover_image_id_media_id_fk" FOREIGN KEY ("cover_image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "portfolio_categories_locales" ADD CONSTRAINT "portfolio_categories_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."portfolio_categories"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "portfolios_images" ADD CONSTRAINT "portfolios_images_image_id_media_id_fk" FOREIGN KEY ("image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "portfolios_images" ADD CONSTRAINT "portfolios_images_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."portfolios"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "portfolios_images_locales" ADD CONSTRAINT "portfolios_images_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."portfolios_images"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "portfolios" ADD CONSTRAINT "portfolios_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "portfolios" ADD CONSTRAINT "portfolios_category_id_portfolio_categories_id_fk" FOREIGN KEY ("category_id") REFERENCES "public"."portfolio_categories"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "portfolios" ADD CONSTRAINT "portfolios_cover_image_id_media_id_fk" FOREIGN KEY ("cover_image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "portfolios" ADD CONSTRAINT "portfolios_seo_og_image_id_media_id_fk" FOREIGN KEY ("seo_og_image_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  ALTER TABLE "portfolios_locales" ADD CONSTRAINT "portfolios_locales_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."portfolios"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "portfolios_texts" ADD CONSTRAINT "portfolios_texts_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."portfolios"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "portfolio_categories_tenant_idx" ON "portfolio_categories" USING btree ("tenant_id");
  CREATE INDEX "portfolio_categories_cover_image_idx" ON "portfolio_categories" USING btree ("cover_image_id");
  CREATE INDEX "portfolio_categories_updated_at_idx" ON "portfolio_categories" USING btree ("updated_at");
  CREATE INDEX "portfolio_categories_created_at_idx" ON "portfolio_categories" USING btree ("created_at");
  CREATE UNIQUE INDEX "portfolio_categories_locales_locale_parent_id_unique" ON "portfolio_categories_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "portfolios_images_order_idx" ON "portfolios_images" USING btree ("_order");
  CREATE INDEX "portfolios_images_parent_id_idx" ON "portfolios_images" USING btree ("_parent_id");
  CREATE INDEX "portfolios_images_image_idx" ON "portfolios_images" USING btree ("image_id");
  CREATE UNIQUE INDEX "portfolios_images_locales_locale_parent_id_unique" ON "portfolios_images_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "portfolios_tenant_idx" ON "portfolios" USING btree ("tenant_id");
  CREATE INDEX "portfolios_category_idx" ON "portfolios" USING btree ("category_id");
  CREATE INDEX "portfolios_cover_image_idx" ON "portfolios" USING btree ("cover_image_id");
  CREATE INDEX "portfolios_seo_seo_og_image_idx" ON "portfolios" USING btree ("seo_og_image_id");
  CREATE INDEX "portfolios_updated_at_idx" ON "portfolios" USING btree ("updated_at");
  CREATE INDEX "portfolios_created_at_idx" ON "portfolios" USING btree ("created_at");
  CREATE UNIQUE INDEX "portfolios_locales_locale_parent_id_unique" ON "portfolios_locales" USING btree ("_locale","_parent_id");
  CREATE INDEX "portfolios_texts_order_parent" ON "portfolios_texts" USING btree ("order","parent_id");
  ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_portfolio_categories_fk" FOREIGN KEY ("portfolio_categories_id") REFERENCES "public"."portfolio_categories"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_portfolios_fk" FOREIGN KEY ("portfolios_id") REFERENCES "public"."portfolios"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "payload_locked_documents_rels_portfolio_categories_id_idx" ON "payload_locked_documents_rels" USING btree ("portfolio_categories_id");
  CREATE INDEX "payload_locked_documents_rels_portfolios_id_idx" ON "payload_locked_documents_rels" USING btree ("portfolios_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "portfolio_categories" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "portfolio_categories_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "portfolios_images" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "portfolios_images_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "portfolios" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "portfolios_locales" DISABLE ROW LEVEL SECURITY;
  ALTER TABLE "portfolios_texts" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "portfolio_categories" CASCADE;
  DROP TABLE "portfolio_categories_locales" CASCADE;
  DROP TABLE "portfolios_images" CASCADE;
  DROP TABLE "portfolios_images_locales" CASCADE;
  DROP TABLE "portfolios" CASCADE;
  DROP TABLE "portfolios_locales" CASCADE;
  DROP TABLE "portfolios_texts" CASCADE;
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_portfolio_categories_fk";
  
  ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_portfolios_fk";
  
  DROP INDEX "payload_locked_documents_rels_portfolio_categories_id_idx";
  DROP INDEX "payload_locked_documents_rels_portfolios_id_idx";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "portfolio_categories_id";
  ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "portfolios_id";
  DROP TYPE "public"."enum_portfolios_status";`)
}
