/**
 * Migration: product.image (single) → product.images (array)
 * Run: node migrate-images.mjs
 */
import 'dotenv/config'
import { createClient } from '@sanity/client'

const sanity = createClient({
  projectId: process.env.SANITY_PROJECT_ID,
  dataset: process.env.SANITY_DATASET || 'production',
  apiVersion: '2024-01-01',
  useCdn: false,
  token: process.env.SANITY_API_TOKEN,
})

const products = await sanity.fetch('*[_type == "product" && defined(image)]{ _id, image }')

console.log(`Found ${products.length} products to migrate`)

for (const p of products) {
  console.log(`Migrating ${p._id}...`)
  await sanity
    .patch(p._id)
    .set({ images: [p.image] })
    .unset(['image'])
    .commit()
  console.log(`  ✓ done`)
}

console.log('Migration complete!')
