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

const IMAGE_SIZES = {
  LARGE: 720,
  SMALL: 500
};
async function convertSvgToPng(svgContent, sizeType = 'LARGE') {
  try {
    const size = IMAGE_SIZES[sizeType] || IMAGE_SIZES.LARGE;
    const tempSvgPath = path.join(__dirname, './temp/temp.svg');
    const timestampSuffix = Date.now();
    const outputPngPath = path.join(__dirname, `./temp/output_${timestampSuffix}.png`);

    // Ensure temp directory exists
    if (!fs.existsSync(path.dirname(tempSvgPath))) {
      fs.mkdirSync(path.dirname(tempSvgPath), { recursive: true });
    }

    // Write SVG to temporary file
    fs.writeFileSync(tempSvgPath, svgContent);

    // Convert using sharp (better for production)
    await sharp(tempSvgPath)
      .resize(720, 300, {
        fit: 'inside',
        background: { r: 255, g: 255, b: 255, alpha: 0 }
      })
      .toFile(outputPngPath);

    // Read the PNG file
    const pngBuffer = fs.readFileSync(outputPngPath);

    // Clean up temp files
    fs.unlinkSync(tempSvgPath);
    fs.unlinkSync(outputPngPath);

    return pngBuffer;
  } catch (error) {
    console.error('Error converting SVG to PNG:', error);
    throw error;
  }
}


module.exports = {
   
  convertSvgToPng,
  IMAGE_SIZES
};