"""Generate the canonical Vaultfire brand asset set from the master logo."""
from PIL import Image
import numpy as np
from pathlib import Path

SRC = '/home/user/workspace/5C6D8E4A-7453-4E0D-84D6-64CD81F75294.jpeg'
OUT = Path('/home/user/workspace/vaultfire-brand')
OUT.mkdir(exist_ok=True)

master = Image.open(SRC).convert('RGB')
print(f'Master: {master.size}')

# --- 1. Shield-only (for X avatar, app icons, favicons) ---
# From audit: shield is at x=113-315 approx (shape bbox, not full left half)
# Re-measure shield precisely
a = np.array(master.convert('L'))
mask = a > 80
# Shield is a contiguous blob before a gap (space between shield and wordmark)
# Left half scan: find right edge of shield = first column after shield where a gap appears
left_cols = mask[:, :512].any(axis=0)
# Find shield right edge by scanning for the gap
in_shield = False
shield_right = 0
for x in range(100, 512):
    if left_cols[x]:
        in_shield = True
        shield_right = x
    elif in_shield and not left_cols[x] and not left_cols[x+1:x+20].any():
        shield_right = x - 1
        break
shield_left = np.where(left_cols)[0][0]
shield_top = np.where(mask.any(axis=1))[0][0]
shield_bot = np.where(mask.any(axis=1))[0][-1]
print(f'Shield bbox: ({shield_left},{shield_top}) - ({shield_right},{shield_bot})')

# Crop shield with padding so it's square and balanced
shield = master.crop((shield_left, shield_top, shield_right + 1, shield_bot + 1))
print(f'Shield raw: {shield.size}')

# Pad to square with black bg for social icons
def pad_to_square(img, bg=(0,0,0), pad_ratio=0.2):
    w, h = img.size
    side = max(w, h)
    side = int(side * (1 + pad_ratio * 2))
    canvas = Image.new('RGB', (side, side), bg)
    canvas.paste(img, ((side - w) // 2, (side - h) // 2))
    return canvas

# Shield on black square — canonical app/avatar icon
shield_square_black = pad_to_square(shield, (0,0,0), 0.25)
shield_square_black.save(OUT / 'shield-square-black.png')
# Resize variants
for size in (16, 32, 48, 64, 128, 180, 192, 256, 400, 512, 1024):
    shield_square_black.resize((size, size), Image.LANCZOS).save(OUT / f'shield-{size}.png')
    print(f'  shield-{size}.png')

# --- 2. Shield with transparent background (for overlays/docs) ---
shield_rgba = Image.new('RGBA', shield.size, (0,0,0,0))
shield_arr = np.array(shield.convert('L'))
# White pixels become white with full alpha, black stays transparent
alpha = np.clip((shield_arr.astype(int) - 30) * 3, 0, 255).astype(np.uint8)
rgba = np.zeros((shield.height, shield.width, 4), dtype=np.uint8)
rgba[..., 0] = 255  # R white
rgba[..., 1] = 255  # G white
rgba[..., 2] = 255  # B white
rgba[..., 3] = alpha
Image.fromarray(rgba, 'RGBA').save(OUT / 'shield-white-transparent.png')
print('  shield-white-transparent.png')

# Inverted (black shield on transparent) for light backgrounds
rgba_black = rgba.copy()
rgba_black[..., 0:3] = 0
Image.fromarray(rgba_black, 'RGBA').save(OUT / 'shield-black-transparent.png')
print('  shield-black-transparent.png')

# --- 3. Full logo (shield + wordmark) on black — for README headers ---
# Full logo bbox = 113-910, 404-618
full = master.crop((113 - 40, 404 - 40, 910 + 40, 618 + 40))
full.save(OUT / 'logo-full-black.png')
print(f'  logo-full-black.png ({full.size})')

# Full logo transparent (white on transparent)
full_gray = np.array(full.convert('L'))
full_alpha = np.clip((full_gray.astype(int) - 30) * 3, 0, 255).astype(np.uint8)
full_rgba = np.zeros((full.height, full.width, 4), dtype=np.uint8)
full_rgba[..., 0:3] = 255
full_rgba[..., 3] = full_alpha
Image.fromarray(full_rgba, 'RGBA').save(OUT / 'logo-full-white-transparent.png')
print('  logo-full-white-transparent.png')

# Inverted: black on transparent for light backgrounds
full_rgba_k = full_rgba.copy()
full_rgba_k[..., 0:3] = 0
Image.fromarray(full_rgba_k, 'RGBA').save(OUT / 'logo-full-black-transparent.png')
print('  logo-full-black-transparent.png')

# --- 4. Favicon .ico (multi-res) ---
ico_img = shield_square_black.resize((256, 256), Image.LANCZOS)
ico_img.save(OUT / 'favicon.ico', format='ICO', sizes=[(16,16),(32,32),(48,48),(64,64),(128,128),(256,256)])
print('  favicon.ico')

# --- 5. OG image (1200x630 for social previews) ---
og = Image.new('RGB', (1200, 630), (0, 0, 0))
# Place full logo centered, sized to ~60% width
logo_for_og = full.resize((720, int(720 * full.height / full.width)), Image.LANCZOS)
og.paste(logo_for_og, ((1200 - logo_for_og.width) // 2, (630 - logo_for_og.height) // 2))
og.save(OUT / 'og-image.png')
print(f'  og-image.png (1200x630)')

# --- 6. Apple touch icons ---
shield_square_black.resize((180, 180), Image.LANCZOS).save(OUT / 'apple-touch-icon.png')
print('  apple-touch-icon.png (180x180)')

# --- 7. Expo mobile app icons ---
# icon.png: 1024x1024 main app icon
shield_square_black.resize((1024, 1024), Image.LANCZOS).save(OUT / 'icon.png')
# splash-icon.png: 512x512 usually white space around it
shield_square_black.resize((512, 512), Image.LANCZOS).save(OUT / 'splash-icon.png')
# adaptive-icon.png: 1024x1024 with padding for Android safe zone (use wider padding)
adaptive = pad_to_square(shield, (0,0,0), 0.5).resize((1024,1024), Image.LANCZOS)
adaptive.save(OUT / 'adaptive-icon.png')
print('  icon.png / splash-icon.png / adaptive-icon.png (Expo)')

print('\nAll assets built in:', OUT)
PY
