#!/usr/bin/env python3
"""Test script for Gemini image analysis - v2."""
import sys
sys.path.insert(0, '.')

from pathlib import Path
from PIL import Image, ImageDraw, ImageFont

# Create a simple test image (cartoon face)
def create_test_face_image(output_path: Path):
    """Create a simple cartoon face for testing."""
    img = Image.new('RGB', (400, 400), color='#FFE4C4')  # Bisque background
    draw = ImageDraw.Draw(img)

    # Face outline (circle)
    draw.ellipse([50, 50, 350, 350], fill='#FFDAB9', outline='#8B4513', width=3)

    # Eyes
    draw.ellipse([120, 140, 170, 190], fill='white', outline='#333')
    draw.ellipse([230, 140, 280, 190], fill='white', outline='#333')
    draw.ellipse([135, 155, 155, 175], fill='#333')  # Left pupil
    draw.ellipse([245, 155, 265, 175], fill='#333')  # Right pupil

    # Eyebrows
    draw.arc([110, 115, 180, 155], 0, 180, fill='#8B4513', width=3)
    draw.arc([220, 115, 290, 155], 0, 180, fill='#8B4513', width=3)

    # Nose
    draw.polygon([(200, 180), (185, 240), (215, 240)], outline='#CD853F', fill=None)

    # Mouth (smile)
    draw.arc([140, 240, 260, 310], 0, 180, fill='#CD5C5C', width=4)

    # Hair
    draw.arc([50, 20, 350, 150], 180, 360, fill='#8B4513', width=30)

    output_path.parent.mkdir(parents=True, exist_ok=True)
    img.save(output_path)
    print(f"Created test image: {output_path}")
    return output_path


def test_face_analysis():
    """Test the face analysis with Gemini."""
    from src.gemini_client import analyze_face_image

    # Create test image
    test_image_path = Path("uploads/test_cartoon_face.png")
    create_test_face_image(test_image_path)

    print("\n" + "="*50)
    print("Testing Gemini Face Analysis")
    print("="*50)

    try:
        result = analyze_face_image(test_image_path)
        print("\nAnalysis Result:")
        print("-"*40)
        print(result["analysis"])
        print("-"*40)
        print(f"\nImage path: {result['image_path']}")
        return True
    except Exception as e:
        print(f"Error during analysis: {e}")
        return False


if __name__ == "__main__":
    success = test_face_analysis()
    print(f"\n{'✓ Test passed!' if success else '✗ Test failed!'}")
