#!/usr/bin/env python3
"""Generate VideoBot logo using NanoBanana."""

import os
import sys
from pathlib import Path

# Add scripts to path
sys.path.insert(0, str(Path(__file__).parent))

from laozhang_proxy import call_nanobanana_laozhang
from dotenv import load_dotenv

load_dotenv(Path(__file__).parent.parent / ".env")

LAOZHANG_KEY = os.environ.get("LAOZHANG_KEY")

def load_wb_logo():
    """Load Wildberries logo as base image."""
    wb_path = Path(__file__).parent.parent / "wb.png"
    if wb_path.exists():
        with open(wb_path, 'rb') as f:
            return f.read()
    return create_base_image()

# Create a simple white/purple background as input
# NanoBanana works better with some input image
def create_base_image():
    """Create a simple gradient base image."""
    from PIL import Image
    import io

    # Create 1024x1024 image with Wildberries purple gradient
    img = Image.new('RGB', (1024, 1024), '#8B5CF6')

    # Save to bytes
    buffer = io.BytesIO()
    img.save(buffer, format='PNG')
    return buffer.getvalue()

def generate_logo(prompt: str, output_path: str, aspect_ratio: str = "1:1"):
    """Generate logo with NanoBanana."""

    base_image = load_wb_logo()

    print(f"Generating logo with prompt:")
    print(f"  {prompt[:100]}...")
    print(f"  Aspect ratio: {aspect_ratio}")

    result_data, result_mime = call_nanobanana_laozhang(
        image_data=base_image,
        prompt=prompt,
        api_key=LAOZHANG_KEY,
        mime_type="image/png",
        aspect_ratio=aspect_ratio,
        image_size="1K"
    )

    if result_data:
        # Determine extension from mime type
        ext = "png" if "png" in result_mime else "jpg"
        output_file = f"{output_path}.{ext}"

        with open(output_file, 'wb') as f:
            f.write(result_data)
        print(f"Saved: {output_file}")
        return output_file
    else:
        print("Generation failed")
        return None


if __name__ == "__main__":
    # VideoBot logo prompt - based on WB logo style
    prompt = """
    Create a Telegram bot avatar logo for "VideoBot" brand.

    MUST incorporate the Wildberries "wb" logo style from the input image:
    - Same rounded square shape with rounded corners
    - Same purple-magenta gradient background
    - White icons on purple background

    Design concept:
    - LARGE vintage film camera or cinema projector as main icon (takes 60-70% of space)
    - Small "SM" letters integrated into the camera design
    - Camera should be clearly visible even at small avatar size
    - Bold, thick white lines for camera icon
    - Clean minimalist style like Wildberries app icon

    The camera/projector is the MAIN element, not text.
    Circular Telegram avatar format.
    """

    output = "/home/videobot/dev/bot_logo"

    # Generate a few variations
    for i in range(3):
        print(f"\n=== Variation {i+1} ===")
        generate_logo(prompt, f"{output}_{i+1}", aspect_ratio="1:1")
