# Image processing utilities
from PIL import Image
from io import BytesIO
from typing import Dict, Tuple

class ImageProcessor:
    """Image processing and optimization."""

    THUMBNAILS = {
        'small': (150, 150),
        'medium': (300, 300),
        'large': (600, 600),
    }

    @staticmethod
    async def generate_thumbnails(content: bytes) -> Dict[str, bytes]:
        """Generate thumbnails for an image."""
        try:
            img = Image.open(BytesIO(content))
            thumbnails = {}

            for size_name, (width, height) in ImageProcessor.THUMBNAILS.items():
                img.thumbnail((width, height), Image.Resampling.LANCZOS)
                output = BytesIO()
                img.save(output, format='JPEG', quality=85, optimize=True)
                thumbnails[size_name] = output.getvalue()

            return thumbnails
        except Exception:
            return {}

    @staticmethod
    async def optimize_image(content: bytes, quality: int = 85) -> bytes:
        """Optimize image for web."""
        try:
            img = Image.open(BytesIO(content))
            output = BytesIO()
            img.save(output, format='JPEG', quality=quality, optimize=True)
            return output.getvalue()
        except Exception:
            return content
