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

export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
   CREATE TABLE "users_sessions" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"created_at" timestamp(3) with time zone,
  	"expires_at" timestamp(3) with time zone NOT NULL
  );
  
  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 "users_sessions" ADD CONSTRAINT "users_sessions_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;
  CREATE INDEX "users_sessions_order_idx" ON "users_sessions" USING btree ("_order");
  CREATE INDEX "users_sessions_parent_id_idx" ON "users_sessions" USING btree ("_parent_id");`)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
   ALTER TABLE "users_sessions" DISABLE ROW LEVEL SECURITY;
  DROP TABLE "users_sessions" CASCADE;
  ALTER TABLE "announcement" ALTER COLUMN "date" SET DEFAULT '2025-08-31T09:35:59.090Z';
  ALTER TABLE "calendar" ALTER COLUMN "date" SET DEFAULT '2025-08-31T09:35:59.222Z';
  ALTER TABLE "classifieds" ALTER COLUMN "until" SET DEFAULT '2025-09-26T09:35:59.313Z';`)
}
