import os
from PIL import Image, ImageDraw

def apply_first_logo_branding():
    first_logo_path = "/Users/apple/.gemini/antigravity-ide/brain/822556bd-47a7-4c07-8975-803618af0a3a/media__1781948467684.png"
    # Base rider image is the one with the black badge we created (v3)
    rider_image_v3 = "public/images/slider-courier-mask-purple-v3.png"
    rider_image_v4 = "public/images/slider-courier-mask-purple-v4.png"
    
    # 1. Load the first logo (has white text and transparent background)
    logo_img = Image.open(first_logo_path).convert("RGBA")
    logo_w, logo_h = logo_img.size
    
    # 2. Save transparent copy as logo.png (white text, transparent background)
    logo_img.save("public/logo.png", "PNG")
    logo_img.save("public/images/logo.png", "PNG")
    print("Saved logo.png (transparent white logo) to public/ and public/images/")
    
    # 3. Create transparent purple logo for Navbar (convert white text to purple #662582 / RGB 102, 37, 130)
    logo_purple = Image.new("RGBA", (logo_w, logo_h))
    pixels_orig = logo_img.load()
    pixels_purple = logo_purple.load()
    
    for x in range(logo_w):
        for y in range(logo_h):
            r, g, b, a = pixels_orig[x, y]
            if a > 0:
                # If pixel is white/near white (R > 230, G > 230, B > 230)
                if r > 230 and g > 230 and b > 230:
                    # Convert to purple, preserving original alpha channel
                    pixels_purple[x, y] = (102, 37, 130, a)
                else:
                    pixels_purple[x, y] = (r, g, b, a)
            else:
                pixels_purple[x, y] = (0, 0, 0, 0)
                
    logo_purple.save("public/logo-purple.png", "PNG")
    logo_purple.save("public/images/logo-purple.png", "PNG")
    print("Saved logo-purple.png (transparent purple logo) to public/ and public/images/")
    
    # 4. Create transparent pure white logo (convert all logo pixels to white, preserving alpha)
    logo_white = Image.new("RGBA", (logo_w, logo_h))
    pixels_white = logo_white.load()
    
    for x in range(logo_w):
        for y in range(logo_h):
            r, g, b, a = pixels_orig[x, y]
            if a > 0:
                pixels_white[x, y] = (255, 255, 255, a)
            else:
                pixels_white[x, y] = (0, 0, 0, 0)
                
    # 5. Crop and create Favicon from the circle icon
    # Bounding box of the solid circle icon: X: 0 to 39, Y: 4 to 43 (size 40x40)
    icon_crop = logo_img.crop((0, 4, 40, 44))
    icon_w, icon_h = icon_crop.size
    
    # Draw a solid white circle background behind the favicon
    favicon_img = Image.new("RGBA", (40, 40), (0, 0, 0, 0))
    draw_fav = ImageDraw.Draw(favicon_img)
    
    # Draw solid white circle matching the border of the icon
    margin = 1
    draw_fav.ellipse(
        [margin, margin, 40 - margin, 40 - margin],
        fill=(255, 255, 255, 255)
    )
    
    # Paste the purple logo icon on top of the white circle background
    favicon_img.paste(icon_crop, (0, 0), icon_crop)
    
    # Resize to standard sizes and save
    favicon_32 = favicon_img.resize((32, 32), Image.Resampling.LANCZOS)
    favicon_48 = favicon_img.resize((48, 48), Image.Resampling.LANCZOS)
    
    # Save favicon.ico to public/ and src/app/
    favicon_32.save("public/favicon.ico", format="ICO", sizes=[(32, 32), (48, 48)])
    favicon_32.save("src/app/favicon.ico", format="ICO", sizes=[(32, 32), (48, 48)])
    
    # Save PNG versions
    favicon_48.save("public/favicon.png", "PNG")
    favicon_48.save("public/apple-icon.png", "PNG")
    print("Saved favicon.ico, favicon.png, and apple-icon.png to public/ and src/app/")
    
    # 6. Overlay the new white logo onto the delivery rider's backpack
    rider_img = Image.open(rider_image_v3).convert("RGBA")
    
    # Draw a fresh black rounded rectangle to clear the previous logo inside the badge
    # Center on the bag at x = 887, y = 270
    badge_w = 148
    badge_h = 38
    badge_left = 887 - (badge_w // 2)
    badge_top = 270 - (badge_h // 2)
    badge_right = badge_left + badge_w
    badge_bottom = badge_top + badge_h
    
    draw_rider = ImageDraw.Draw(rider_img)
    draw_rider.rounded_rectangle(
        [badge_left, badge_top, badge_right, badge_bottom],
        radius=8,
        fill=(18, 2, 23, 255) # matches --brand-dark
    )
    
    # Resize the new white Nearle logo (original ratio 275x45 -> width 120, height 20)
    logo_w = 120
    logo_h = 20
    resized_white_logo = logo_white.resize((logo_w, logo_h), Image.Resampling.LANCZOS)
    
    logo_left = 887 - (logo_w // 2)
    logo_top = 270 - (logo_h // 2)
    
    # Paste logo on top of the black badge
    rider_img.paste(resized_white_logo, (logo_left, logo_top), resized_white_logo)
    
    # Save output
    rider_img.save(rider_image_v4, "PNG")
    print(f"Created updated rider image with solid logo at {rider_image_v4}")

if __name__ == "__main__":
    apply_first_logo_branding()
