import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function main() {
  const properties = await prisma.property.findMany()
  
  const replacements: Record<string, string> = {
    'https://images.unsplash.com/photo-1500382017468-9049fed747ef?w=800&q=80': 'https://images.pexels.com/photos/30557705/pexels-photo-30557705.jpeg?auto=compress&cs=tinysrgb&w=800',
    'https://images.unsplash.com/photo-1500382017468-9049fed747ef?w=1600&q=80': 'https://images.pexels.com/photos/30557705/pexels-photo-30557705.jpeg?auto=compress&cs=tinysrgb&w=1600',
    'https://images.pexels.com/photos/1580466/pexels-photo-1580466.jpeg?auto=compress&cs=tinysrgb&w=800': 'https://images.pexels.com/photos/30557705/pexels-photo-30557705.jpeg?auto=compress&cs=tinysrgb&w=800'
  }

  for (const p of properties) {
    if (p.photos && p.photos.length > 0) {
      let updated = false
      const newPhotos = p.photos.map(photo => {
        if (replacements[photo]) {
          updated = true
          return replacements[photo]
        }
        
        // Also just replace any unsplash or 1580466 with 30557705 if it's the exact sunset one
        if (photo.includes('1500382017468') || photo.includes('1580466')) {
            updated = true
            return 'https://images.pexels.com/photos/30557705/pexels-photo-30557705.jpeg?auto=compress&cs=tinysrgb&w=800'
        }
        
        return photo
      })

      if (updated) {
        await prisma.property.update({
          where: { id: p.id },
          data: { photos: newPhotos }
        })
        console.log(`Updated property ${p.id}`)
      }
    }
  }
}

main()
  .catch(console.error)
  .finally(() => prisma.$disconnect())
