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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-19T07:51:55.082Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-19T07:51:55.418Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-17T07:51:55.487Z';
  ALTER TABLE "pages_rels" ADD COLUMN "church_id" uuid;
  ALTER TABLE "_pages_v_rels" ADD COLUMN "church_id" uuid;
  ALTER TABLE "pages_rels" ADD CONSTRAINT "pages_rels_church_fk" FOREIGN KEY ("church_id") REFERENCES "public"."church"("id") ON DELETE cascade ON UPDATE no action;
  ALTER TABLE "_pages_v_rels" ADD CONSTRAINT "_pages_v_rels_church_fk" FOREIGN KEY ("church_id") REFERENCES "public"."church"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "pages_rels_church_id_idx" ON "pages_rels" USING btree ("church_id");
  CREATE INDEX "_pages_v_rels_church_id_idx" ON "_pages_v_rels" USING btree ("church_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "pages_rels" DROP CONSTRAINT "pages_rels_church_fk";
  
  ALTER TABLE "_pages_v_rels" DROP CONSTRAINT "_pages_v_rels_church_fk";
  
  DROP INDEX "pages_rels_church_id_idx";
  DROP INDEX "_pages_v_rels_church_id_idx";
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-04-19T07:28:46.077Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-04-19T07:28:46.368Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-05-17T07:28:46.426Z';
  ALTER TABLE "pages_rels" DROP COLUMN "church_id";
  ALTER TABLE "_pages_v_rels" DROP COLUMN "church_id";`)
}
