from PIL import Image, ImageDraw, ImageFont
import numpy as np

FONT_REG    = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
FONT_BOLD   = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
FONT_ITALIC = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Oblique.ttf"

# Brand colors
NAVY        = (26, 82, 118)
BLUE        = (46, 134, 193)
GRAY_TEXT   = (68, 68, 68)
GRAY_LABEL  = (153, 153, 153)
GRAY_ITALIC = (127, 140, 141)
DIVIDER     = (213, 216, 220)
BORDER_LINE = (26, 82, 118)

W, H = 700, 160
scale = 2
SW, SH = W * scale, H * scale

canvas = Image.new("RGBA", (SW, SH), (255, 255, 255, 255))
draw = ImageDraw.Draw(canvas)

# ── Remove black background from logo ──
logo_src = Image.open('Endabash.jpeg').convert("RGBA")
arr = np.array(logo_src, dtype=np.float32)
rgb = arr[:, :, :3]
brightness = rgb.max(axis=2)
alpha = np.clip((brightness - 25) / (60 - 25), 0, 1)
alpha = (alpha * 255).astype(np.uint8)
result = arr.copy().astype(np.uint8)
result[:, :, 3] = alpha
logo = Image.fromarray(result, 'RGBA')

# Auto-crop to tight bounding box, then resize
bbox = logo.getbbox()
logo_cropped = logo.crop(bbox)
target_h = 88 * scale
ratio = target_h / logo_cropped.height
target_w = int(logo_cropped.width * ratio)
logo_resized = logo_cropped.resize((target_w, target_h), Image.LANCZOS)

logo_x = 18 * scale
logo_y = (SH - target_h) // 2
canvas.paste(logo_resized, (logo_x, logo_y), logo_resized)

# ── Vertical divider ──
div_x = logo_x + target_w + 22 * scale
draw.line([(div_x, 18*scale), (div_x, SH - 18*scale)], fill=BORDER_LINE, width=3)

# ── Text helpers ──
def put_text(text, x, y, font_path, size, color):
    fnt = ImageFont.truetype(font_path, size * scale)
    draw.text((x, y), text, font=fnt, fill=color)
    bb = draw.textbbox((x, y), text, font=fnt)
    return bb[3]

def contact_row(label, value, x, y, link=False):
    fnt_l = ImageFont.truetype(FONT_REG, 9 * scale)
    fnt_v = ImageFont.truetype(FONT_REG, 10 * scale)
    draw.text((x, y), label, font=fnt_l, fill=GRAY_LABEL)
    lb = draw.textbbox((x, y), label, font=fnt_l)
    vx = lb[2] + 5 * scale
    draw.text((vx, y - scale), value, font=fnt_v, fill=BLUE if link else GRAY_TEXT)
    vb = draw.textbbox((vx, y), value, font=fnt_v)
    return vb[3] + 4 * scale

# ── Text content ──
tx, ty = div_x + 20 * scale, 16 * scale

y = put_text("Mabruk Hasnu", tx, ty, FONT_BOLD, 14, NAVY)
y += 3 * scale
fnt_title = ImageFont.truetype(FONT_REG, 10 * scale)
draw.text((tx, y), "MANAGING DIRECTOR", font=fnt_title, fill=BLUE)
y = draw.textbbox((tx, y), "MANAGING DIRECTOR", font=fnt_title)[3] + 7 * scale
y = put_text("Endabash Freighters Limited", tx, y, FONT_BOLD, 11, NAVY)
y += 2 * scale
y = put_text("Clearing and Forwarding Specialist", tx, y, FONT_ITALIC, 9, GRAY_ITALIC)
y += 7 * scale
draw.line([(tx, y), (SW - 18*scale, y)], fill=DIVIDER, width=scale)
y += 7 * scale

y = contact_row("Tel:",     "+255 716 609 600",              tx, y, link=True)
y = contact_row("Email:",   "mabruk@endabashfreighters.com", tx, y, link=True)
y = contact_row("Web:",     "www.endabashfreighters.com",    tx, y, link=True)
y = contact_row("Address:", "Dar es Salaam, Tanzania",       tx, y)

# ── Save ──
final_rgba = canvas.resize((W, H), Image.LANCZOS)
final_rgb  = Image.new("RGB", (W, H), (255, 255, 255))
final_rgb.paste(final_rgba, mask=final_rgba.split()[3])
final_rgb.save('Endabash_Signature_MabrukHasnu.png', 'PNG', dpi=(150, 150))