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

/**
 * Migration: Add processed fields for Posts featuredVideo
 *
 * Adds columns for storing processed video metadata:
 * - processedEmbedUrl: Generated embed URL with privacy mode
 * - extractedVideoId: Extracted video ID (e.g. YouTube video ID)
 * - platform: Detected platform (youtube, vimeo, etc.)
 * - thumbnailUrl: Auto-generated thumbnail URL
 */
export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
  await db.execute(sql`
  ALTER TABLE "posts" ADD COLUMN IF NOT EXISTS "featured_video_processed_embed_url" varchar;
  ALTER TABLE "posts" ADD COLUMN IF NOT EXISTS "featured_video_extracted_video_id" varchar;
  ALTER TABLE "posts" ADD COLUMN IF NOT EXISTS "featured_video_platform" varchar;
  ALTER TABLE "posts" ADD COLUMN IF NOT EXISTS "featured_video_thumbnail_url" varchar;
  `)
}

export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
  await db.execute(sql`
  ALTER TABLE "posts" DROP COLUMN IF EXISTS "featured_video_thumbnail_url";
  ALTER TABLE "posts" DROP COLUMN IF EXISTS "featured_video_platform";
  ALTER TABLE "posts" DROP COLUMN IF EXISTS "featured_video_extracted_video_id";
  ALTER TABLE "posts" DROP COLUMN IF EXISTS "featured_video_processed_embed_url";
  `)
}
