import dotenv from 'dotenv';
import { neon } from '@neondatabase/serverless';

dotenv.config({ path: '.env.local' });

async function fixLorcanaImages() {
  const sql = neon(process.env.POSTGRES_URL);

  try {
    console.log('🏰 Fixing Lorcana card images...');

    // Get all Lorcana cards with image URLs
    const lorcanaCards = await sql`
      SELECT id, name, image_url, stock_image_url 
      FROM cards 
      WHERE game = 'Lorcana' AND image_url IS NOT NULL
    `;

    console.log(`📸 Found ${lorcanaCards.length} Lorcana cards to process`);

    let updatedCount = 0;
    for (const card of lorcanaCards) {
      let needsUpdate = false;
      let newImageUrl = card.image_url;
      let newStockImageUrl = card.stock_image_url;

      // Check if we're using a small image as the main image
      if (card.image_url && card.image_url.includes('-716.webp')) {
        // Replace with 1024px version for main image
        newImageUrl = card.image_url.replace('-716.webp', '-1024.webp');
        newStockImageUrl = card.image_url; // Keep 716px as thumbnail
        needsUpdate = true;
      } else if (card.image_url && card.image_url.includes('-512.webp')) {
        // Replace with 1024px version for main image
        newImageUrl = card.image_url.replace('-512.webp', '-1024.webp');
        newStockImageUrl = card.image_url; // Keep 512px as thumbnail
        needsUpdate = true;
      }

      // Also check if both image_url and stock_image_url are the same small image
      if (card.image_url === card.stock_image_url && card.image_url && 
          (card.image_url.includes('-716.webp') || card.image_url.includes('-512.webp'))) {
        // They're the same small image, fix this
        if (card.image_url.includes('-716.webp')) {
          newImageUrl = card.image_url.replace('-716.webp', '-1024.webp');
          newStockImageUrl = card.image_url; // Keep original as thumbnail
        } else if (card.image_url.includes('-512.webp')) {
          newImageUrl = card.image_url.replace('-512.webp', '-1024.webp');
          newStockImageUrl = card.image_url; // Keep original as thumbnail
        }
        needsUpdate = true;
      }

      if (needsUpdate) {
        await sql`
          UPDATE cards 
          SET image_url = ${newImageUrl}, 
              stock_image_url = ${newStockImageUrl},
              updated_at = CURRENT_TIMESTAMP
          WHERE id = ${card.id}
        `;
        
        console.log(`✅ Updated ${card.name}: ${card.image_url} → ${newImageUrl}`);
        updatedCount++;
      }
    }

    console.log(`🎉 Updated ${updatedCount} Lorcana cards with higher quality images`);
    
    if (updatedCount === 0) {
      console.log('ℹ️  No cards needed updating - they may already have high quality images');
    }

  } catch (error) {
    console.error('❌ Error fixing Lorcana images:', error);
  }
}

// Run the script
fixLorcanaImages()
  .then(() => {
    console.log('✅ Script completed');
    process.exit(0);
  })
  .catch((error) => {
    console.error('❌ Script failed:', error);
    process.exit(1);
  }); 