import { MigrateUpArgs, MigrateDownArgs, sql } from '@payloadcms/db-postgres'

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
ALTER TABLE "pages_rels" ADD COLUMN IF NOT EXISTS "authors_id" integer;
ALTER TABLE "pages_rels" ADD COLUMN IF NOT EXISTS "posts_id" integer;
ALTER TABLE "pages_rels" ADD COLUMN IF NOT EXISTS "locations_id" integer;
ALTER TABLE "pages_rels" ADD COLUMN IF NOT EXISTS "partners_id" integer;
ALTER TABLE "pages_rels" ADD COLUMN IF NOT EXISTS "jobs_id" integer;
ALTER TABLE "pages_rels" ADD COLUMN IF NOT EXISTS "downloads_id" integer;
ALTER TABLE "pages_rels" ADD COLUMN IF NOT EXISTS "events_id" integer;

DO $$ BEGIN
  ALTER TABLE "pages_rels" ADD CONSTRAINT "pages_rels_authors_fk" FOREIGN KEY ("authors_id") REFERENCES "public"."authors"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_rels" ADD CONSTRAINT "pages_rels_posts_fk" FOREIGN KEY ("posts_id") REFERENCES "public"."posts"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_rels" ADD CONSTRAINT "pages_rels_locations_fk" FOREIGN KEY ("locations_id") REFERENCES "public"."locations"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_rels" ADD CONSTRAINT "pages_rels_partners_fk" FOREIGN KEY ("partners_id") REFERENCES "public"."partners"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_rels" ADD CONSTRAINT "pages_rels_jobs_fk" FOREIGN KEY ("jobs_id") REFERENCES "public"."jobs"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_rels" ADD CONSTRAINT "pages_rels_downloads_fk" FOREIGN KEY ("downloads_id") REFERENCES "public"."downloads"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  ALTER TABLE "pages_rels" ADD CONSTRAINT "pages_rels_events_fk" FOREIGN KEY ("events_id") REFERENCES "public"."events"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

CREATE INDEX IF NOT EXISTS "pages_rels_authors_id_idx" ON "pages_rels" USING btree ("authors_id");
CREATE INDEX IF NOT EXISTS "pages_rels_posts_id_idx" ON "pages_rels" USING btree ("posts_id");
CREATE INDEX IF NOT EXISTS "pages_rels_locations_id_idx" ON "pages_rels" USING btree ("locations_id");
CREATE INDEX IF NOT EXISTS "pages_rels_partners_id_idx" ON "pages_rels" USING btree ("partners_id");
CREATE INDEX IF NOT EXISTS "pages_rels_jobs_id_idx" ON "pages_rels" USING btree ("jobs_id");
CREATE INDEX IF NOT EXISTS "pages_rels_downloads_id_idx" ON "pages_rels" USING btree ("downloads_id");
CREATE INDEX IF NOT EXISTS "pages_rels_events_id_idx" ON "pages_rels" USING btree ("events_id");
  `)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  // No-op: data-preserving migration for pages_rels columns.
}
