#!/usr/bin/env python3
"""
Script pour générer les logos BTPXpress dans différentes variantes
"""

from PIL import Image, ImageDraw, ImageFont
import os

def create_logo_base(width, height, bg_color, text_color, border_color=None):
    """Crée un logo de base BTPXpress"""
    img = Image.new('RGBA', (width, height), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # Calculer les dimensions du badge
    badge_size = min(width, height) - 10
    badge_x = (width - badge_size) // 2
    badge_y = (height - badge_size) // 2
    
    # Dessiner le badge principal
    draw.rounded_rectangle(
        [badge_x, badge_y, badge_x + badge_size, badge_y + badge_size],
        radius=badge_size // 8,
        fill=bg_color,
        outline=border_color,
        width=2 if border_color else 0
    )
    
    # Calculer la taille de police
    font_size = badge_size // 3
    try:
        font = ImageFont.truetype("arial.ttf", font_size)
    except:
        font = ImageFont.load_default()
    
    # Dessiner le texte "BTP"
    text = "BTP"
    bbox = draw.textbbox((0, 0), text, font=font)
    text_width = bbox[2] - bbox[0]
    text_height = bbox[3] - bbox[1]
    
    text_x = badge_x + (badge_size - text_width) // 2
    text_y = badge_y + (badge_size - text_height) // 2
    
    draw.text((text_x, text_y), text, fill=text_color, font=font)
    
    return img

def create_appname_base(width, height, text_color, bg_transparent=True):
    """Crée le nom de l'application BTPXpress"""
    bg_color = (0, 0, 0, 0) if bg_transparent else (255, 255, 255, 255)
    img = Image.new('RGBA', (width, height), bg_color)
    draw = ImageDraw.Draw(img)
    
    # Calculer la taille de police
    font_size = height // 2
    try:
        font = ImageFont.truetype("arial.ttf", font_size)
    except:
        font = ImageFont.load_default()
    
    # Dessiner le texte "Xpress"
    text = "Xpress"
    bbox = draw.textbbox((0, 0), text, font=font)
    text_width = bbox[2] - bbox[0]
    text_height = bbox[3] - bbox[1]
    
    text_x = (width - text_width) // 2
    text_y = (height - text_height) // 2
    
    draw.text((text_x, text_y), text, fill=text_color, font=font)
    
    return img

def generate_all_logos():
    """Génère tous les logos nécessaires"""
    output_dir = "../public/layout/images/logo"
    os.makedirs(output_dir, exist_ok=True)
    
    # Couleurs BTPXpress
    orange = (249, 115, 22, 255)  # #f97316
    white = (255, 255, 255, 255)
    black = (0, 0, 0, 255)
    gray = (107, 114, 128, 255)  # #6b7280
    
    # Dimensions basées sur les fichiers existants
    logo_size = (64, 64)  # Taille standard pour les logos
    appname_size = (120, 24)  # Taille pour le nom de l'app
    
    # Générer les logos principaux
    logos = {
        'logo-light.png': create_logo_base(*logo_size, white, orange, orange),
        'logo-dark.png': create_logo_base(*logo_size, orange, white),
        'logo-gray.png': create_logo_base(*logo_size, gray, white),
    }
    
    # Générer les noms d'application
    appnames = {
        'appname-light.png': create_appname_base(*appname_size, white),
        'appname-dark.png': create_appname_base(*appname_size, black),
        'appname-gray.png': create_appname_base(*appname_size, gray),
    }

    # Créer un logo principal optimisé pour remplacer btpxpress-logo.png
    main_logo = create_logo_base(128, 128, orange, white)

    # Sauvegarder tous les fichiers
    all_images = {**logos, **appnames, 'btpxpress-logo-optimized.png': main_logo}
    
    for filename, img in all_images.items():
        filepath = os.path.join(output_dir, filename)
        img.save(filepath, 'PNG', optimize=True)
        print(f"✅ Généré: {filename}")
    
    print(f"\n🎉 Tous les logos ont été générés dans {output_dir}")

if __name__ == "__main__":
    generate_all_logos()
