#!/usr/bin/env python3
"""Test stylu white_on_green z auto-dopasowaniem czcionki"""

import requests
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO

# Pobierz testowy obrazek
url = "https://twojerowery.shop/ro64/wp-content/uploads/2026/02/allegro-image-14694.jpg"
response = requests.get(url, timeout=30)
image = Image.open(BytesIO(response.content))

# Różne przykładowe tytuły
test_titles = [
    "Rower MTB górski Kross",
    "Rower elektryczny Męski Damski 250W",
    "ROWEREK DZIECIĘCY 16 CALI MIRAVO",
    "Lekki rower dziecięcy woom EXPLORE 4",
]

price = "688 PLN"

# Font paths
font_paths = [
    "/usr/share/fonts/truetype/montserrat/Montserrat-Black.ttf",
    "/usr/share/fonts/truetype/montserrat/Montserrat-Bold.ttf",
    "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
]

def get_font(size):
    for path in font_paths:
        try:
            return ImageFont.truetype(path, size)
        except:
            continue
    return ImageFont.load_default()

def add_overlay_green(image, title, price, max_width_pct=0.85):
    """Dodaje nakładkę w stylu green z auto-dopasowaniem czcionki"""
    if image.mode != 'RGBA':
        image = image.convert('RGBA')
    
    overlay = Image.new('RGBA', image.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(overlay)
    
    width, height = image.size
    max_title_width = width * max_width_pct
    
    # Cena font
    price_font_size = int(width * 0.09)
    price_font = get_font(price_font_size)
    
    # Auto-dopasowanie fontu tytułu - ZMIEŃSZAJ JEŚLI SIĘ NIE MIEŚCI
    title_font_size = int(width * 0.06)  # Start z 6%
    title_font = get_font(title_font_size)
    
    # Zmniejszaj dopóki się nie zmieści
    while title_font_size > int(width * 0.025):  # Min ~2.5%
        bbox = draw.textbbox((0, 0), title, font=title_font)
        text_width = bbox[2] - bbox[0]
        if text_width <= max_title_width:
            break
        title_font_size -= int(width * 0.002)
        title_font = get_font(title_font_size)
    
    # Kolorystyka GREEN
    bg_color = (30, 120, 50, 230)  # Zielone tło
    text_color = (255, 255, 255, 255)  # Biały tekst
    
    # === TYTUŁ NA ŚRODKU (jedna linia!) ===
    bbox = draw.textbbox((0, 0), title, font=title_font)
    text_width = bbox[2] - bbox[0]
    text_height = bbox[3] - bbox[1]
    text_offset_y = bbox[1]
    
    padding_x = int(width * 0.02)
    padding_y = int(height * 0.012)
    
    title_box_width = text_width + 2 * padding_x
    title_box_height = text_height + 2 * padding_y + abs(text_offset_y)
    
    # ŚRODEK obrazka
    title_x = (width - title_box_width) // 2
    title_y = (height - title_box_height) // 2 - int(height * 0.08)
    
    corner_radius = int(height * 0.012)
    
    # Tło tytułu
    draw.rounded_rectangle(
        [title_x, title_y, title_x + title_box_width, title_y + title_box_height],
        radius=corner_radius,
        fill=bg_color
    )
    
    # Tekst tytułu (jedna linia, wyśrodkowany)
    title_text_x = title_x + (title_box_width - text_width) // 2
    title_text_y = title_y + (title_box_height - text_height) // 2 - text_offset_y
    
    draw.text((title_text_x, title_text_y), title, font=title_font, fill=text_color)
    
    # === CENA NA DOLE ===
    bbox = draw.textbbox((0, 0), price, font=price_font)
    price_text_width = bbox[2] - bbox[0]
    price_text_height = bbox[3] - bbox[1]
    price_offset_y = bbox[1]
    
    price_padding_x = int(width * 0.012)
    price_padding_y = int(height * 0.008)
    
    price_box_width = price_text_width + 2 * price_padding_x
    price_box_height = price_text_height + 2 * price_padding_y + abs(price_offset_y)
    
    price_x = (width - price_box_width) // 2
    price_y = height - price_box_height - int(height * 0.03)
    
    price_corner_radius = int(height * 0.015)
    
    # Tło ceny
    draw.rounded_rectangle(
        [price_x, price_y, price_x + price_box_width, price_y + price_box_height],
        radius=price_corner_radius,
        fill=bg_color
    )
    
    # Tekst ceny
    price_text_x = price_x + (price_box_width - price_text_width) // 2
    price_text_y = price_y + (price_box_height - price_text_height) // 2 - price_offset_y
    
    draw.text((price_text_x, price_text_y), price, font=price_font, fill=text_color)
    
    return Image.alpha_composite(image, overlay)


# Generuj wersje testowe
for i, title in enumerate(test_titles, 1):
    result = add_overlay_green(image.copy(), title, price)
    result_rgb = Image.new('RGB', result.size, (255, 255, 255))
    result_rgb.paste(result, mask=result.split()[-1])
    safe_name = title.replace(" ", "_")[:30]
    result_rgb.save(f'/var/www/files/green_test_{i}.jpg', 'JPEG', quality=95)
    print(f"Test {i}: {title}")
    print(f"  Link: https://backupserwer.shop/green_test_{i}.jpg")

print("\n✅ Gotowe! Tytuły są w jednej linii - czcionka automatycznie się zmniejsza.")
