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

async function generateIcon512() {
	try {
		console.log('Generating icon-512.png from icon-source.png...');

		// Check if source file exists
		if (!fs.existsSync('static/icon-source.png')) {
			console.error('Error: static/icon-source.png does not exist');
			process.exit(1);
		}

		// Generate 512x512 icon
		await sharp('static/icon-source.png')
			.resize(512, 512, {
				fit: 'contain',
				background: { r: 0, g: 0, b: 0, alpha: 0 }
			})
			.png()
			.toFile('static/icon-512.png');

		console.log('✓ Generated static/icon-512.png');

		// Verify the result
		const metadata = await sharp('static/icon-512.png').metadata();
		const stats = fs.statSync('static/icon-512.png');

		console.log(`  Dimensions: ${metadata.width}x${metadata.height}`);
		console.log(`  Format: ${metadata.format}`);
		console.log(`  Size: ${Math.round(stats.size / 1024)}KB`);

		// Validate
		if (metadata.width !== 512 || metadata.height !== 512) {
			console.error('Error: Invalid dimensions');
			process.exit(1);
		}
		if (metadata.format !== 'png') {
			console.error('Error: Invalid format');
			process.exit(1);
		}
		if (stats.size > 200 * 1024) {
			console.error('Error: File size exceeds 200KB');
			process.exit(1);
		}

		console.log('✓ Validation passed');
		process.exit(0);
	} catch (error) {
		console.error('Error generating icon:', error.message);
		process.exit(1);
	}
}

generateIcon512();
