#!/usr/bin/env python3
"""BUG-HUNT — simple, effective: dark band + bold text + drop shadow."""

from PIL import Image, ImageDraw, ImageFont, ImageFilter
import numpy as np
import os

orig = Image.open("/root/.openclaw/workspace/output/bug-hunt-evidence-board-v2-2.png").convert("RGBA")
w, h = orig.size

font_path = "/tmp/clash-display/ClashDisplay_Complete/Fonts/OTF/ClashDisplay-Bold.otf"
text = "BUG-HUNT"

# Size: ~75% width
target_width = int(w * 0.75)
best_size = 80
for size in range(60, 250, 5):
    font = ImageFont.truetype(font_path, size)
    tracking = int(size * 0.10)
    total = sum(font.getbbox(c)[2] - font.getbbox(c)[0] for c in text) + tracking * (len(text) - 1)
    if total <= target_width:
        best_size = size
    else:
        break

font = ImageFont.truetype(font_path, best_size)
print(f"Font size: {best_size}")

tracking = int(best_size * 0.10)
char_widths = [font.getbbox(c)[2] - font.getbbox(c)[0] for c in text]
total_text_w = sum(char_widths) + tracking * (len(text) - 1)
start_x = (w - total_text_w) // 2
text_h = font.getbbox(text)[3] - font.getbbox(text)[1]
y_pos = int(h * 0.07)

# Step 1: Full-width dark gradient band behind text area
band = Image.new("RGBA", (w, h), (0, 0, 0, 0))
bd = ImageDraw.Draw(band)
band_center = y_pos + text_h // 2
band_half = text_h + 50  # generous padding

for y in range(max(0, band_center - band_half), min(h, band_center + band_half)):
    dist = abs(y - band_center) / band_half
    # Smooth falloff
    alpha = int(140 * (1 - dist * dist))  # quadratic falloff, peak 140/255 ≈ 55% opacity
    bd.line([(0, y), (w, y)], fill=(5, 8, 20, alpha))

darkened = Image.alpha_composite(orig, band)

# Step 2: Drop shadow
shadow = Image.new("RGBA", (w, h), (0, 0, 0, 0))
sd = ImageDraw.Draw(shadow)
x = start_x
for i, c in enumerate(text):
    sd.text((x + 3, y_pos + 4), c, font=font, fill=(0, 0, 0, 220))
    x += char_widths[i] + tracking
shadow = shadow.filter(ImageFilter.GaussianBlur(radius=8))

# Step 3: White text
text_layer = Image.new("RGBA", (w, h), (0, 0, 0, 0))
td = ImageDraw.Draw(text_layer)
x = start_x
for i, c in enumerate(text):
    td.text((x, y_pos), c, font=font, fill=(255, 255, 255, 255))
    x += char_widths[i] + tracking

# Composite
result = Image.alpha_composite(darkened, shadow)
result = Image.alpha_composite(result, text_layer)

# Step 4: Restore lamp glow ON TOP of text (lamp in front)
orig_np = np.array(orig).astype(float)
result_np = np.array(result).astype(float)
brightness = np.mean(orig_np[:,:,:3], axis=2)

# Create lamp mask - upper left, only very bright pixels (the actual lamp bulb glow)
mask = np.zeros((h, w), dtype=float)
# Lamp bulb is roughly in the area (x: 50-250, y: 50-250) based on typical position
for y_c in range(min(int(h * 0.40), h)):
    for x_c in range(min(int(w * 0.25), w)):
        b = brightness[y_c, x_c]
        if b > 140:  # only the brightest parts (lamp bulb + immediate glow)
            mask[y_c, x_c] = min(1.0, (b - 140) / 80)

mask_img = Image.fromarray((mask * 255).astype(np.uint8))
mask_img = mask_img.filter(ImageFilter.GaussianBlur(radius=5))
mask = np.array(mask_img).astype(float) / 255.0

for c_idx in range(3):
    result_np[:,:,c_idx] = mask * orig_np[:,:,c_idx] + (1 - mask) * result_np[:,:,c_idx]

final = Image.fromarray(result_np.astype(np.uint8))
out = "/root/.openclaw/workspace/output/bug-hunt-evidence-board-final.png"
final.convert("RGB").save(out, "PNG")
print(f"Saved: {out} ({os.path.getsize(out)} bytes)")
