from PIL import Image, ImageDraw, ImageFont
import os

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

def create_blog_image(title, category, color_start, color_end, filename):
    """Create a blog post header image"""
    width, height = 400, 250
    
    # Create image with gradient
    img = Image.new('RGB', (width, height), color=color_start)
    draw = ImageDraw.Draw(img, 'RGBA')
    
    # Create gradient effect
    for y in range(height):
        ratio = y / height
        # Interpolate between colors
        r = int(color_start[0] * (1 - ratio) + color_end[0] * ratio)
        g = int(color_start[1] * (1 - ratio) + color_end[1] * ratio)
        b = int(color_start[2] * (1 - ratio) + color_end[2] * ratio)
        draw.line([(0, y), (width, y)], fill=(r, g, b))
    
    # Add semi-transparent overlay
    draw.rectangle([(0, 0), (width, height)], fill=(0, 0, 0, 30))
    
    # Add category badge
    badge_color = (255, 140, 0, 220)  # Orange with transparency
    draw.rectangle([(15, 15), (15 + len(category) * 12, 40)], fill=badge_color, outline=(255, 255, 255, 100))
    
    # Try to add text, fallback to simple version if font not available
    try:
        font_title = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20)
        font_category = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 12)
    except:
        font_title = ImageFont.load_default()
        font_category = ImageFont.load_default()
    
    # Draw category text
    draw.text((20, 19), category, font=font_category, fill=(255, 255, 255, 255))
    
    # Draw title (wrapped)
    title_text = title[:35] + "..." if len(title) > 35 else title
    draw.text((20, height - 60), title_text, font=font_title, fill=(255, 255, 255, 255))
    
    # Save image
    filepath = os.path.join(images_dir, filename)
    img.save(filepath)
    print(f"Created: {filepath}")

# Define blog images with gradients
blog_images = [
    {
        "title": "Large Language Models",
        "category": "LLM",
        "color_start": (255, 140, 0),      # Orange
        "color_end": (255, 107, 0),        # Dark orange
        "filename": "blog-llm.png"
    },
    {
        "title": "AI Image Generation",
        "category": "Generative AI",
        "color_start": (255, 107, 0),      # Dark orange
        "color_end": (200, 100, 0),        # Darker orange
        "filename": "blog-generative-ai.png"
    },
    {
        "title": "Video Generation",
        "category": "Video AI",
        "color_start": (107, 114, 128),    # Gray
        "color_end": (255, 140, 0),        # Orange
        "filename": "blog-video-ai.png"
    }
]

# Create all images
for blog in blog_images:
    create_blog_image(
        blog["title"],
        blog["category"],
        blog["color_start"],
        blog["color_end"],
        blog["filename"]
    )

print("✅ All blog images created successfully!")
