import os
from PIL import Image

def generate_icons():
    img_path = '/root/Apps/Camila/public/assets/camila_prof.png'
    if not os.path.exists(img_path):
        print(f"Error: {img_path} not found.")
        return

    img = Image.open(img_path)
    width, height = img.size
    print(f"Original image size: {width}x{height}")

    # Crop to a square. The face is usually in the upper half of the image.
    # Original is 498x741. Let's crop a square of 498x498.
    # Let's start the crop from y=50 to y=548 to keep the face well-centered.
    size = min(width, height)
    left = (width - size) // 2
    top = 50  # Offset down slightly to avoid cropping the top of the head
    right = left + size
    bottom = top + size

    if bottom > height:
        bottom = height
        top = height - size

    cropped_img = img.crop((left, top, right, bottom))
    print(f"Cropped to square: {cropped_img.size}")

    # Target directory
    assets_dir = '/root/Apps/Camila/public/assets'
    os.makedirs(assets_dir, exist_ok=True)

    # Save sizes
    sizes = {
        180: 'apple-touch-icon.png',
        192: 'icon-192.png',
        512: 'icon-512.png'
    }

    for sz, filename in sizes.items():
        resized = cropped_img.resize((sz, sz), Image.Resampling.LANCZOS)
        out_path = os.path.join(assets_dir, filename)
        resized.save(out_path, 'PNG')
        print(f"Saved {sz}x{sz} icon to {out_path}")

if __name__ == '__main__':
    generate_icons()
