import sharp from 'sharp';

/**
 * Generate a thumbnail from an image buffer
 *
 * CRITICAL: Uses sharp.rotate() first to handle EXIF orientation from mobile photos.
 * Without this, photos taken in portrait mode may appear rotated.
 *
 * @param buffer - Original image buffer
 * @param size - Thumbnail size (default 150px)
 * @returns Thumbnail buffer as JPEG
 */
export async function generateThumbnail(buffer: Buffer, size: number = 150): Promise<Buffer> {
	return sharp(buffer)
		.rotate() // Auto-rotate based on EXIF orientation
		.resize(size, size, {
			fit: 'cover',
			position: 'center',
			withoutEnlargement: true
		})
		.jpeg({ quality: 80 })
		.toBuffer();
}
