import os
from PIL import Image, ImageDraw

def update_brand_assets_v3():
    new_logo_path = "/Users/apple/.gemini/antigravity-ide/brain/822556bd-47a7-4c07-8975-803618af0a3a/media__1781946188768.png"
    # Base rider image: we load from the original to ensure clean edit
    rider_image_base = "public/images/slider-courier-mask.png"
    # We will output to v3
    rider_image_out = "public/images/slider-courier-mask-purple-v3.png"
    
    # 1. Load the new logo
    logo_img = Image.open(new_logo_path).convert("RGBA")
    
    # 2. Crop the logo to the exact content area
    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 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")
    
    # 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")
    
    # 5. Crop and create Favicon from the circle icon
    icon_crop = logo_img.crop((84, 43, 150, 84))
    icon_w, icon_h = icon_crop.size
    
    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)
                
    favicon_32 = transparent_icon.resize((32, 32), Image.Resampling.LANCZOS)
    favicon_48 = transparent_icon.resize((48, 48), Image.Resampling.LANCZOS)
    favicon_32.save("public/favicon.ico", format="ICO", sizes=[(32, 32), (48, 48)])
    favicon_48.save("public/favicon.png", "PNG")
    favicon_48.save("public/apple-icon.png", "PNG")
    
    # 6. Load base yellow rider, recolor yellow to purple (recolor scooter panels, helmet, shirt, bag)
    # We do a fresh recolor to make sure we don't have residual artifacts
    import colorsys
    img_rider = Image.open(rider_image_base).convert("RGBA")
    pixels_rider = img_rider.load()
    width_rider, height_rider = img_rider.size
    
    target_hue = 280.0 / 360.0
    
    for x in range(width_rider):
        for y in range(height_rider):
            r, g, b, a = pixels_rider[x, y]
            if a == 0:
                continue
            h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
            is_yellow = False
            if 0.10 <= h <= 0.23 and s >= 0.35 and v >= 0.30:
                is_yellow = True
            elif 0.07 <= h < 0.10 and s >= 0.70 and v >= 0.50:
                is_yellow = True
                
            if is_yellow:
                new_h = target_hue
                new_s = min(s * 1.0, 1.0)
                new_v = v
                new_r, new_g, new_b = colorsys.hsv_to_rgb(new_h, new_s, new_v)
                pixels_rider[x, y] = (int(new_r * 255), int(new_g * 255), int(new_b * 255), a)
                
    # 7. Erase old GOMOTO logo using linear interpolation
    left_x = 804
    right_x = 970
    
    for y in range(140, 425):
        color_left = pixels_rider[left_x, y]
        color_right = pixels_rider[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)
            pixels_rider[x, y] = (r, g, b, a)
            
    # 8. Create a premium black badge (rounded rectangle)
    # Center on the bag at x = 887, y = 275
    # Width = 144, Height = 36 (radius = 8)
    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 = ImageDraw.Draw(img_rider)
    # Draw dark premium badge: pure black with full opacity (#0b010f or #000000)
    # A tiny bit of rounding gives it a sleek rubber-badge look
    draw.rounded_rectangle(
        [badge_left, badge_top, badge_right, badge_bottom],
        radius=8,
        fill=(18, 2, 23, 255) # matches --brand-dark color perfectly!
    )
    
    # 9. Resize and draw white Nearle logo inside the badge
    # Logo size inside the badge: width = 120, height = 20
    logo_w = 120
    logo_h = 20
    resized_white_logo = transparent_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
    img_rider.paste(resized_white_logo, (logo_left, logo_top), resized_white_logo)
    
    # Save output
    img_rider.save(rider_image_out, "PNG")
    print(f"Created updated rider image with black badge at {rider_image_out}")

if __name__ == "__main__":
    update_brand_assets_v3()
