#!/usr/bin/env python3
"""Add BUG-HUNT text in Clash Display Bold white on top of the evidence board image."""

from PIL import Image, ImageDraw, ImageFont
import os

# Load image
img = Image.open("/root/.openclaw/workspace/output/bug-hunt-evidence-board-v2-2.png")
w, h = img.size
print(f"Image size: {w}x{h}")

# Load Clash Display Bold
font_path = "/tmp/clash-display/ClashDisplay_Complete/Fonts/OTF/ClashDisplay-Bold.otf"

# We want "BUG-HUNT" very wide across the top with good spacing
# Try different sizes to find one that spans most of the width with padding
text = "BUG-HUNT"

# Use letter spacing by manually placing each character
# First find a good font size - aim for text to span ~85% of image width
target_width = int(w * 0.85)

# Try sizes
best_size = 80
for size in range(60, 200, 5):
    font = ImageFont.truetype(font_path, size)
    bbox = font.getbbox(text)
    text_w = bbox[2] - bbox[0]
    # Add extra tracking (letter spacing) - roughly 15% of font size per gap
    tracking = int(size * 0.15)
    total_w = text_w + tracking * (len(text) - 1)
    if total_w <= target_width:
        best_size = size
    else:
        break

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

# Calculate character positions with wide tracking
tracking = int(best_size * 0.2)  # wide tracking
chars = list(text)
char_widths = []
for c in chars:
    bbox = font.getbbox(c)
    char_widths.append(bbox[2] - bbox[0])

total_text_w = sum(char_widths) + tracking * (len(chars) - 1)

# Center horizontally
start_x = (w - total_text_w) // 2

# Vertical position - in the upper area with good padding
# Position it so it sits in the dark area above the corkboard
# with some overlap behind the lamp
bbox = font.getbbox(text)
text_h = bbox[3] - bbox[1]
y_pos = int(h * 0.06)  # ~6% from top, good spacing

draw = ImageDraw.Draw(img)

# Draw each character with tracking
x = start_x
for i, c in enumerate(chars):
    # White text with slight transparency effect via subtle shadow
    # Draw shadow first
    draw.text((x + 2, y_pos + 2), c, font=font, fill=(0, 0, 0, 120))
    # Draw main text in white
    draw.text((x, y_pos), c, font=font, fill=(255, 255, 255, 240))
    x += char_widths[i] + tracking

# Save
out = "/root/.openclaw/workspace/output/bug-hunt-evidence-board-final.png"
img.save(out, "PNG")
print(f"Saved: {out} ({os.path.getsize(out)} bytes)")

# Also save a version with full opacity white for cleaner look
img2 = Image.open("/root/.openclaw/workspace/output/bug-hunt-evidence-board-v2-2.png")
draw2 = ImageDraw.Draw(img2)
x = start_x
for i, c in enumerate(chars):
    draw2.text((x + 2, y_pos + 2), c, font=font, fill=(0, 0, 0, 80))
    draw2.text((x, y_pos), c, font=font, fill=(255, 255, 255))
    x += char_widths[i] + tracking

out2 = "/root/.openclaw/workspace/output/bug-hunt-evidence-board-final-clean.png"
img2.save(out2, "PNG")
print(f"Saved: {out2} ({os.path.getsize(out2)} bytes)")
