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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "blog" RENAME COLUMN "excerpt" TO "content_excerpt";
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2025-09-14T07:56:02.742Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2025-09-14T07:56:02.825Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2025-10-09T07:56:02.879Z';
  ALTER TABLE "blog" ADD COLUMN "configuration_show_on_frontpage" boolean DEFAULT true NOT NULL;
  ALTER TABLE "blog" ADD COLUMN "configuration_display_from_date" timestamp(3) with time zone;
  ALTER TABLE "blog" ADD COLUMN "configuration_display_till_date" timestamp(3) with time zone;
 UPDATE blog_blocks_text SET _path = 'content.content';
 UPDATE blog_blocks_button SET _path = 'content.content';
 UPDATE blog_blocks_contactform SET _path = 'content.content';
 UPDATE blog_blocks_document SET _path = 'content.content';
 UPDATE blog_blocks_gallery SET _path = 'content.content';
 UPDATE blog_blocks_donation SET _path = 'content.content';
`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2025-08-31T10:51:21.316Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2025-08-31T10:51:21.398Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2025-09-26T10:51:21.450Z';
  ALTER TABLE "blog" ADD COLUMN "excerpt" varchar NOT NULL;
  ALTER TABLE "blog" DROP COLUMN "content_excerpt";
  ALTER TABLE "blog" DROP COLUMN "configuration_show_on_frontpage";
  ALTER TABLE "blog" DROP COLUMN "configuration_display_from_date";
  ALTER TABLE "blog" DROP COLUMN "configuration_display_till_date";
  UPDATE blog_blocks_text SET _path = 'content';
  UPDATE blog_blocks_button SET _path = 'content';
  UPDATE blog_blocks_contactform SET _path = 'content';
  UPDATE blog_blocks_document SET _path = 'content';
  UPDATE blog_blocks_gallery SET _path = 'content';
  UPDATE blog_blocks_donation SET _path = 'content';
`)
}
