from PIL import Image, ImageDraw
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_dna_molecular_banner():
    """Create an AI banner with DNA/molecular structure theme"""
    width, height = 1920, 800
    
    # Create base image with dark gradient
    img = Image.new('RGB', (width, height), color=(8, 12, 28))
    draw = ImageDraw.Draw(img, 'RGBA')
    
    # Create gradient background - dark to dark teal
    for y in range(height):
        ratio = y / height
        r = int(8 + (15 - 8) * ratio)
        g = int(12 + (40 - 12) * ratio)
        b = int(28 + (80 - 28) * ratio)
        draw.line([(0, y), (width, y)], fill=(r, g, b))
    
    # Draw DNA double helix
    helix_center_x = width // 2
    helix_amplitude = 200
    helix_frequency = 0.01
    
    # Left and right strands
    left_strand = []
    right_strand = []
    
    for x in range(0, width, 2):
        # Wave pattern for DNA
        offset = math.sin(x * helix_frequency) * helix_amplitude
        
        left_y = height // 2 - 100 + int(50 * math.sin(x * 0.003)) + int(offset)
        right_y = height // 2 + 100 - int(50 * math.sin(x * 0.003)) - int(offset)
        
        left_strand.append((x, left_y))
        right_strand.append((x, right_y))
    
    # Draw helix strands with gradient effect
    for i in range(len(left_strand) - 1):
        # Left strand
        draw.line([left_strand[i], left_strand[i + 1]], 
                 fill=(255, 140, 0, 200), width=3)
        # Right strand
        draw.line([right_strand[i], right_strand[i + 1]], 
                 fill=(100, 200, 255, 200), width=3)
    
    # Draw connecting rings (base pairs)
    for i in range(0, len(left_strand), 20):
        if i < len(left_strand) and i < len(right_strand):
            x1, y1 = left_strand[i]
            x2, y2 = right_strand[i]
            
            # Connection line
            draw.line([(x1, y1), (x2, y2)], fill=(138, 43, 226, 150), width=2)
            
            # Node circles
            radius = 8
            draw.ellipse([(x1 - radius, y1 - radius), (x1 + radius, y1 + radius)],
                        fill=(255, 140, 0, 200))
            draw.ellipse([(x2 - radius, y2 - radius), (x2 + radius, y2 + radius)],
                        fill=(100, 200, 255, 200))
    
    # Add molecular atoms around the helix
    atoms = [
        (200, 150, 6, (255, 140, 0, 150)),
        (1700, 200, 8, (100, 200, 255, 140)),
        (300, 650, 7, (138, 43, 226, 130)),
        (1600, 600, 6, (255, 140, 0, 140)),
        (900, 100, 5, (100, 200, 255, 150)),
        (1200, 700, 7, (138, 43, 226, 130)),
    ]
    
    for ax, ay, size, color in atoms:
        # Outer shell
        draw.ellipse([(ax - size - 5, ay - size - 5),
                     (ax + size + 5, ay + size + 5)],
                    outline=color, width=1)
        # Inner atom
        draw.ellipse([(ax - size, ay - size),
                     (ax + size, ay + size)],
                    fill=color)
    
    # Add connecting lines between atoms and helix
    for ax, ay, _, _ in atoms:
        # Find nearest point on helix and connect
        nearest_x = int(ax)
        if 0 <= nearest_x < len(left_strand):
            strand_y = left_strand[nearest_x][1]
            draw.line([(ax, ay), (nearest_x, strand_y)],
                     fill=(255, 140, 0, 80), width=1)
    
    # Add tech grid in background
    grid_spacing = 150
    for x in range(0, width, grid_spacing):
        draw.line([(x, 0), (x, height)], fill=(255, 140, 0, 10), width=1)
    for y in range(0, height, grid_spacing):
        draw.line([(0, y), (width, y)], fill=(255, 140, 0, 10), width=1)
    
    # Add hexagonal patterns (DNA connection indicator)
    hex_size = 30
    for hx in [400, 800, 1200, 1600]:
        for hy in [200, 600]:
            # Draw hexagon
            angles = [i * 60 for i in range(6)]
            hex_points = [(hx + hex_size * math.cos(math.radians(a)),
                          hy + hex_size * math.sin(math.radians(a)))
                         for a in angles]
            draw.polygon(hex_points, outline=(138, 43, 226, 100), width=1)
    
    # Save image
    filepath = os.path.join(images_dir, "hero-ai-dna.png")
    img.save(filepath)
    print(f"Created: {filepath}")
    return filepath

# Create the banner
create_dna_molecular_banner()
print("DNA molecular AI banner created successfully!")
