from PIL import Image, ImageDraw, ImageFont
import math
import os

# Create images directory if it doesn't exist
images_dir = "public/images"
os.makedirs(images_dir, exist_ok=True)

def create_ai_hero_banner():
    """Create a cool AI-themed hero banner with neural networks and gradients"""
    width, height = 1920, 800
    
    # Create base image with dark background
    img = Image.new('RGB', (width, height), color=(15, 23, 42))
    draw = ImageDraw.Draw(img, 'RGBA')
    
    # Create gradient background - dark blue to darker blue
    for y in range(height):
        ratio = y / height
        # Gradient from dark blue (top) to very dark (bottom)
        r = int(15 + (10 - 15) * ratio)
        g = int(23 + (20 - 23) * ratio)
        b = int(42 + (60 - 42) * ratio)
        draw.line([(0, y), (width, y)], fill=(r, g, b))
    
    # Add subtle grid pattern
    grid_spacing = 80
    grid_color = (255, 140, 0, 8)  # Very transparent orange
    for x in range(0, width, grid_spacing):
        draw.line([(x, 0), (x, height)], fill=grid_color, width=1)
    for y in range(0, height, grid_spacing):
        draw.line([(0, y), (width, y)], fill=grid_color, width=1)
    
    # Draw neural network nodes and connections
    nodes = [
        (200, 200), (400, 150), (600, 250), (800, 180),
        (1000, 300), (1200, 200), (1400, 280), (1600, 150),
        (1800, 320)
    ]
    
    # Draw connections between nodes
    connection_color = (255, 140, 0, 40)  # Transparent orange
    for i, node in enumerate(nodes):
        for j in range(i + 1, min(i + 3, len(nodes))):
            if j < len(nodes):
                draw.line([node, nodes[j]], fill=connection_color, width=2)
    
    # Draw nodes with glow effect
    node_color = (255, 140, 0, 255)  # Bright orange
    glow_color = (255, 140, 0, 80)   # Semi-transparent orange
    
    for node in nodes:
        # Draw glow
        draw.ellipse(
            [(node[0] - 20, node[1] - 20), (node[0] + 20, node[1] + 20)],
            fill=glow_color
        )
        # Draw node
        draw.ellipse(
            [(node[0] - 8, node[1] - 8), (node[0] + 8, node[1] + 8)],
            fill=node_color
        )
    
    # Add flowing lines (representing data flow)
    line_color = (255, 140, 0, 60)
    for i in range(0, width, 300):
        points = []
        for x in range(i, min(i + 300, width), 10):
            y = height // 2 + int(50 * math.sin(x * 0.01 + i * 0.005))
            points.append((x, y))
        
        if len(points) > 1:
            for j in range(len(points) - 1):
                draw.line([points[j], points[j + 1]], fill=line_color, width=2)
    
    # Add circles at various positions (representing data points)
    for cx, cy in [(300, 600), (900, 100), (1500, 650), (400, 350)]:
        circle_color = (255, 140, 0, 30)
        radius = 60
        draw.ellipse([(cx - radius, cy - radius), (cx + radius, cy + radius)], 
                     outline=circle_color, width=2)
    
    # Add some tech-style rectangles
    rect_color = (255, 140, 0, 25)
    rectangles = [
        (100, 100, 250, 200),
        (1650, 400, 1850, 600),
        (800, 650, 1100, 750),
    ]
    for rect in rectangles:
        draw.rectangle(rect, outline=rect_color, width=2)
    
    # Add subtle text overlay
    try:
        font_big = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 72)
        font_small = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 36)
    except:
        font_big = ImageFont.load_default()
        font_small = ImageFont.load_default()
    
    # Add gradient text effect (centered)
    text_color = (255, 255, 255, 200)
    text_x = width // 2 - 400
    text_y = height // 2 - 100
    
    draw.text((text_x, text_y), "AI-Ready Future", font=font_big, fill=text_color)
    draw.text((text_x, text_y + 100), "Neural Networks • Machine Learning • Generative AI", 
              font=font_small, fill=(255, 140, 0, 180))
    
    # Save image
    filepath = os.path.join(images_dir, "hero-ai-banner.png")
    img.save(filepath)
    print(f"Created: {filepath}")
    return filepath

# Create the banner
create_ai_hero_banner()
print("AI hero banner created successfully!")
