import os
from PIL import Image, ImageOps

def update_brand_assets():
    new_logo_path = "/Users/apple/.gemini/antigravity-ide/brain/822556bd-47a7-4c07-8975-803618af0a3a/media__1781946188768.png"
    rider_image_path = "public/images/slider-courier-mask-purple.png"
    
    # 1. Load the new logo
    logo_img = Image.open(new_logo_path).convert("RGBA")
    
    # 2. Crop the logo to the exact content area
    # Bounding box of the entire logo was:
    # X: 85 to 359 (width 275), Y: 40 to 83 (height 44)
    # Let's crop with a tiny padding: left=82, top=38, right=362, bottom=85
    cropped_logo = logo_img.crop((82, 38, 362, 85))
    cropped_w, cropped_h = cropped_logo.size
    
    # 3. Create transparent purple logo (strip white/off-white background)
    transparent_purple = Image.new("RGBA", (cropped_w, cropped_h))
    pixels_purple = cropped_logo.load()
    pixels_tp = transparent_purple.load()
    
    for x in range(cropped_w):
        for y in range(cropped_h):
            r, g, b, a = pixels_purple[x, y]
            # If the pixel is white or very close to white background (RGB > 248)
            if r >= 248 and g >= 248 and b >= 248:
                pixels_tp[x, y] = (0, 0, 0, 0)
            else:
                pixels_tp[x, y] = (r, g, b, a)
                
    transparent_purple.save("public/logo-purple.png", "PNG")
    transparent_purple.save("public/images/logo-purple.png", "PNG")
    print("Saved logo-purple.png (transparent purple logo) to public/ and public/images/")
    
    # 4. Create transparent white logo (convert all logo pixels to white, preserving alpha)
    transparent_white = Image.new("RGBA", (cropped_w, cropped_h))
    pixels_tw = transparent_white.load()
    
    for x in range(cropped_w):
        for y in range(cropped_h):
            r, g, b, a = pixels_tp[x, y]
            if a > 0:
                pixels_tw[x, y] = (255, 255, 255, a)
            else:
                pixels_tw[x, y] = (0, 0, 0, 0)
                
    transparent_white.save("public/logo.png", "PNG")
    transparent_white.save("public/images/logo.png", "PNG")
    print("Saved logo.png (transparent white logo) to public/ and public/images/")
    
    # 5. Crop and create Favicon from the circle icon
    # Bounding box of the circle icon:
    # X: 85 to 149 (width 65), Y: 44 to 83 (height 40)
    # Let's crop from the original logo at (84, 43, 150, 84)
    icon_crop = logo_img.crop((84, 43, 150, 84))
    icon_w, icon_h = icon_crop.size
    
    # Make background transparent
    transparent_icon = Image.new("RGBA", (icon_w, icon_h))
    pixels_ic = icon_crop.load()
    pixels_ti = transparent_icon.load()
    
    for x in range(icon_w):
        for y in range(icon_h):
            r, g, b, a = pixels_ic[x, y]
            if r >= 248 and g >= 248 and b >= 248:
                pixels_ti[x, y] = (0, 0, 0, 0)
            else:
                pixels_ti[x, y] = (r, g, b, a)
                
    # Resize to a square 32x32 and 48x48
    favicon_32 = transparent_icon.resize((32, 32), Image.Resampling.LANCZOS)
    favicon_48 = transparent_icon.resize((48, 48), Image.Resampling.LANCZOS)
    
    # Save as ICO (favicon.ico)
    favicon_32.save("public/favicon.ico", format="ICO", sizes=[(32, 32), (48, 48)])
    
    # Save as PNG icons
    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/")
    
    # 6. Erase old GOMOTO logo from the backpack bag and overlay white Nearle logo
    rider_img = Image.open(rider_image_path).convert("RGBA")
    rider_pixels = rider_img.load()
    
    # Erase logo area X: 808 to 968, Y: 140 to 425 using linear interpolation between left (804) and right (970) columns
    left_x = 804
    right_x = 970
    
    for y in range(140, 425):
        color_left = rider_pixels[left_x, y]
        color_right = rider_pixels[right_x, y]
        
        width_logo = right_x - left_x - 1
        for x in range(left_x + 1, right_x):
            t = (x - left_x - 1) / width_logo
            r = int(color_left[0] * (1 - t) + color_right[0] * t)
            g = int(color_left[1] * (1 - t) + color_right[1] * t)
            b = int(color_left[2] * (1 - t) + color_right[2] * t)
            a = int(color_left[3] * (1 - t) + color_right[3] * t)
            rider_pixels[x, y] = (r, g, b, a)
            
    print("Erased old GOMOTO logo from delivery bag.")
    
    # Prepare white Nearle logo to be pasted onto the bag
    # Let's resize the white Nearle logo
    # The GOMOTO text was centered. Let's make our Nearle logo width = 120, height = 20
    bag_logo_w = 120
    bag_logo_h = 20
    resized_bag_logo = transparent_white.resize((bag_logo_w, bag_logo_h), Image.Resampling.LANCZOS)
    
    # Center on the bag at x = 887 - (120/2) = 827, y = 284 - (20/2) = 274
    # Let's adjust slightly for visual balance
    paste_x = 827
    paste_y = 265
    
    # Paste the logo with transparency mask
    rider_img.paste(resized_bag_logo, (paste_x, paste_y), resized_bag_logo)
    
    # Save the updated rider image
    rider_img.save(rider_image_path, "PNG")
    print("Successfully overlaid white Nearle logo on rider's backpack and saved.")

if __name__ == "__main__":
    update_brand_assets()
