const sharp = require('sharp');
const fs = require('fs');
const path = require('path');

async function generatePWAIcons() {
  const inputFile = path.join(__dirname, '../public/MONACOUSA-Flags_376x376.png');
  const publicDir = path.join(__dirname, '../public');
  
  console.log('🎨 Generating PWA icons from MonacoUSA logo...');
  
  try {
    // Generate 192x192 icon
    await sharp(inputFile)
      .resize(192, 192, { 
        fit: 'contain',
        background: { r: 255, g: 255, b: 255, alpha: 0 }
      })
      .png()
      .toFile(path.join(publicDir, 'icon-192x192.png'));
    
    console.log('✅ Generated icon-192x192.png');
    
    // Generate 512x512 icon
    await sharp(inputFile)
      .resize(512, 512, { 
        fit: 'contain',
        background: { r: 255, g: 255, b: 255, alpha: 0 }
      })
      .png()
      .toFile(path.join(publicDir, 'icon-512x512.png'));
    
    console.log('✅ Generated icon-512x512.png');
    
    // Generate Apple touch icon (180x180)
    await sharp(inputFile)
      .resize(180, 180, { 
        fit: 'contain',
        background: { r: 255, g: 255, b: 255, alpha: 1 }
      })
      .png()
      .toFile(path.join(publicDir, 'apple-touch-icon.png'));
    
    console.log('✅ Generated apple-touch-icon.png');
    
    // Generate favicon (32x32)
    await sharp(inputFile)
      .resize(32, 32, { 
        fit: 'contain',
        background: { r: 255, g: 255, b: 255, alpha: 0 }
      })
      .png()
      .toFile(path.join(publicDir, 'favicon-32x32.png'));
    
    console.log('✅ Generated favicon-32x32.png');
    
    console.log('🎉 All PWA icons generated successfully!');
    
  } catch (error) {
    console.error('❌ Error generating PWA icons:', error);
    process.exit(1);
  }
}

generatePWAIcons();
