#!/usr/bin/env python3
"""
Create properly sized favicon images from the logo
"""
from PIL import Image
import os

# Paths
logo_path = "/media/pts/Website/PromptTech_Solution_Site/Logo/PTB-logo.png"
public_dir = "/media/pts/Website/PromptTech_Solution_Site/frontend/public"

# Load the original logo
print("Loading logo...")
logo = Image.open(logo_path)
print(f"Original size: {logo.size}")

# Convert to RGBA if needed
if logo.mode != 'RGBA':
    logo = logo.convert('RGBA')

# Define sizes to create
sizes = [
    (16, 16, "favicon-16x16.png"),
    (32, 32, "favicon-32x32.png"),
    (48, 48, "favicon-48x48.png"),
    (64, 64, "favicon-64x64.png"),
    (180, 180, "apple-touch-icon.png"),
    (192, 192, "android-chrome-192x192.png"),
    (512, 512, "android-chrome-512x512.png"),
]

# Create each size
for width, height, filename in sizes:
    # Create a square canvas
    size = (width, height)
    
    # Resize with high quality
    resized = logo.copy()
    resized.thumbnail(size, Image.Resampling.LANCZOS)
    
    # Create a new image with transparent background
    new_img = Image.new('RGBA', size, (255, 255, 255, 0))
    
    # Calculate position to center the logo
    paste_x = (size[0] - resized.size[0]) // 2
    paste_y = (size[1] - resized.size[1]) // 2
    
    # Paste the resized logo
    new_img.paste(resized, (paste_x, paste_y), resized)
    
    # Save
    output_path = os.path.join(public_dir, filename)
    new_img.save(output_path, "PNG", optimize=True)
    print(f"Created: {filename} ({width}x{height})")

# Create favicon.ico with multiple sizes
print("\nCreating favicon.ico with multiple sizes...")
ico_sizes = [(16, 16), (32, 32), (48, 48)]
ico_images = []

for width, height in ico_sizes:
    resized = logo.copy()
    resized.thumbnail((width, height), Image.Resampling.LANCZOS)
    
    # Create centered image
    new_img = Image.new('RGBA', (width, height), (255, 255, 255, 0))
    paste_x = (width - resized.size[0]) // 2
    paste_y = (height - resized.size[1]) // 2
    new_img.paste(resized, (paste_x, paste_y), resized)
    
    ico_images.append(new_img)

# Save as ICO
favicon_path = os.path.join(public_dir, "favicon.ico")
ico_images[0].save(favicon_path, format='ICO', sizes=[(img.width, img.height) for img in ico_images])
print(f"Created: favicon.ico with sizes: {[(img.width, img.height) for img in ico_images]}")

print("\n✅ All favicon images created successfully!")
