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

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

def create_digital_particles_banner():
    """Create an abstract digital particles AI banner with wave effects"""
    width, height = 1920, 800
    
    # Create base image with gradient background
    img = Image.new('RGB', (width, height), color=(10, 15, 35))
    draw = ImageDraw.Draw(img, 'RGBA')
    
    # Create vibrant gradient background - deep blue to purple/teal
    for y in range(height):
        ratio = y / height
        # Gradient from dark blue to teal
        r = int(10 + (20 - 10) * ratio)
        g = int(15 + (50 - 15) * ratio)
        b = int(35 + (100 - 35) * ratio)
        draw.line([(0, y), (width, y)], fill=(r, g, b))
    
    # Add diagonal color accent stripes
    stripe_color = (255, 140, 0, 15)
    for x in range(-height, width, 150):
        points = [(x, 0), (x + height, height)]
        draw.line(points, fill=stripe_color, width=3)
    
    # Generate particle system
    particles = []
    random.seed(42)  # For consistency
    for i in range(150):
        particles.append({
            'x': random.randint(0, width),
            'y': random.randint(0, height),
            'size': random.randint(2, 8),
            'color': random.choice([
                (255, 140, 0, 180),   # Orange
                (100, 200, 255, 150),  # Light blue
                (138, 43, 226, 140),   # Blue violet
            ]),
            'vx': random.uniform(-1, 1),
            'vy': random.uniform(-0.5, 0.5),
        })
    
    # Draw particle clusters and connections
    particle_color_1 = (255, 140, 0, 120)  # Orange
    particle_color_2 = (100, 200, 255, 100)  # Light blue
    
    for i, p in enumerate(particles):
        # Draw particle
        draw.ellipse(
            [(p['x'] - p['size'], p['y'] - p['size']),
             (p['x'] + p['size'], p['y'] + p['size'])],
            fill=p['color']
        )
        
        # Draw connections to nearby particles
        if i % 3 == 0:
            for j in range(i + 1, min(i + 4, len(particles))):
                p2 = particles[j]
                distance = math.sqrt((p['x'] - p2['x'])**2 + (p['y'] - p2['y'])**2)
                if distance < 150:
                    opacity = int(200 * (1 - distance / 150))
                    draw.line(
                        [(p['x'], p['y']), (p2['x'], p2['y'])],
                        fill=(255, 140, 0, opacity // 2),
                        width=1
                    )
    
    # Add flowing wave patterns
    for phase in range(0, width, 200):
        points = []
        for x in range(phase, min(phase + 200, width), 5):
            # Multiple sine waves for complex pattern
            y = (height // 2 + 
                 int(60 * math.sin(x * 0.005 + phase * 0.01)) +
                 int(40 * math.cos(x * 0.003 + phase * 0.015)))
            points.append((x, y))
        
        if len(points) > 1:
            for i in range(len(points) - 1):
                draw.line([points[i], points[i + 1]], 
                         fill=(100, 200, 255, 60), width=2)
    
    # Add glowing orbs at key positions
    orbs = [
        (300, 200, 80, (255, 140, 0, 100)),
        (1500, 600, 100, (100, 200, 255, 80)),
        (800, 150, 60, (138, 43, 226, 90)),
        (1200, 650, 70, (255, 140, 0, 80)),
    ]
    
    for cx, cy, radius, color in orbs:
        # Outer glow
        draw.ellipse(
            [(cx - radius - 20, cy - radius - 20),
             (cx + radius + 20, cy + radius + 20)],
            outline=color,
            width=2
        )
        # Middle glow
        draw.ellipse(
            [(cx - radius, cy - radius),
             (cx + radius, cy + radius)],
            outline=color,
            width=1
        )
        # Inner glow
        draw.ellipse(
            [(cx - radius // 2, cy - radius // 2),
             (cx + radius // 2, cy + radius // 2)],
            fill=color
        )
    
    # Add tech lines
    tech_lines = [
        [(100, 100), (300, 300)],
        [(1800, 200), (1600, 450)],
        [(400, 700), (600, 500)],
        [(1300, 150), (1500, 400)],
    ]
    
    for line in tech_lines:
        draw.line(line, fill=(255, 140, 0, 80), width=3)
    
    # Add scattered squares (digital elements)
    for _ in range(20):
        x = random.randint(0, width)
        y = random.randint(0, height)
        size = random.randint(10, 30)
        draw.rectangle(
            [(x - size, y - size), (x + size, y + size)],
            outline=(100, 200, 255, 60),
            width=1
        )
    
    # Save image
    filepath = os.path.join(images_dir, "hero-ai-particles.png")
    img.save(filepath)
    print(f"Created: {filepath}")
    return filepath

# Create the banner
create_digital_particles_banner()
print("Digital particles AI banner created successfully!")
