const Jimp = require('jimp');

/**
 * Generates a high-quality JPEG thumbnail buffer for externalAdReply.
 * @param {string | Buffer} imagePathOrBuffer - Path to image file, Buffer, or URL.
 * @param {number} [size=150] - Target square size (default 150 for balance between quality and load time).
 * @param {number} [quality=100] - JPEG quality (1-100, default 100 for maximum quality).
 * @returns {Promise<Buffer>} - High-quality thumbnail buffer.
 */
async function generateThumbnail(imagePathOrBuffer, size = 150, quality = 100) {
  const image = await Jimp.read(imagePathOrBuffer);
  return image
    .cover(size, size) // Cover fit for square thumbnail
    .quality(quality) // Set JPEG quality
    .getBufferAsync(Jimp.MIME_JPEG);
}

module.exports = { generateThumbnail };
