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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TABLE "payload_kv" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"key" varchar NOT NULL,
  	"data" jsonb NOT NULL
  );
  
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-02-08T15:57:34.492Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-02-08T15:57:34.801Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-03-07T15:57:34.871Z';
  CREATE UNIQUE INDEX "payload_kv_key_idx" ON "payload_kv" USING btree ("key");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "payload_kv" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "payload_kv" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2026-01-11T10:35:28.881Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2026-01-11T10:35:28.965Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2026-02-05T10:35:29.018Z';`)
}
