#!/usr/bin/env python3
"""
Test script for SVG wireframe generation
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from app import generate_svg_wireframe

def test_svg_generation():
    """Test SVG generation with sample data"""
    
    # Test layout specification
    test_layout = {
        "layout": {
            "page": {"width": 1200, "height": 800},
            "header": {"enabled": True, "height": 72, "elements": ["Logo", "Navigation", "CTA"]},
            "sidebar": {"enabled": True, "width": 240, "position": "left", "elements": ["Menu", "Filters"]},
            "hero": {"enabled": True, "height": 200, "elements": ["Hero Title", "Hero Subtitle", "Button"]},
            "main_content": {
                "sections": [
                    {
                        "type": "grid",
                        "rows": 2,
                        "cols": 3,
                        "height": 200,
                        "elements": ["Card 1", "Card 2", "Card 3", "Card 4", "Card 5", "Card 6"]
                    },
                    {
                        "type": "form",
                        "height": 300,
                        "fields": ["Name", "Email", "Message", "submit"]
                    }
                ]
            },
            "footer": {"enabled": True, "height": 64, "elements": ["Links", "Copyright"]}
        },
        "styling": {
            "theme": "modern",
            "colors": {
                "primary": "#3B82F6",
                "secondary": "#6B7280",
                "background": "#FFFFFF",
                "card": "#F8FAFC",
                "text": "#1F2937"
            },
            "spacing": {"gap": 16, "padding": 20}
        },
        "annotations": {
            "title": "Test Wireframe",
            "description": "Test SVG generation"
        }
    }
    
    try:
        # Generate SVG
        svg_content = generate_svg_wireframe(test_layout)
        
        # Save to file for inspection
        with open('test_wireframe.svg', 'w', encoding='utf-8') as f:
            f.write(svg_content)
        
        print("✅ SVG generation test passed!")
        print(f"Generated SVG: {len(svg_content)} characters")
        print("Saved to: test_wireframe.svg")
        
        # Basic validation
        assert '<svg' in svg_content
        assert 'width="1200"' in svg_content
        assert 'height="800"' in svg_content
        assert 'Logo' in svg_content
        assert 'Card 1' in svg_content
        
        print("✅ SVG validation passed!")
        
    except Exception as e:
        print(f"❌ SVG generation test failed: {e}")
        return False
    
    return True

def test_fallback_spec():
    """Test fallback specification generation"""
    from app import create_fallback_spec
    
    try:
        fallback_spec = create_fallback_spec("Dashboard with header and sidebar")
        
        # Generate SVG from fallback
        svg_content = generate_svg_wireframe(fallback_spec)
        
        with open('test_fallback.svg', 'w', encoding='utf-8') as f:
            f.write(svg_content)
        
        print("✅ Fallback SVG generation test passed!")
        print(f"Generated fallback SVG: {len(svg_content)} characters")
        print("Saved to: test_fallback.svg")
        
    except Exception as e:
        print(f"❌ Fallback SVG generation test failed: {e}")
        return False
    
    return True

if __name__ == '__main__':
    print("🧪 Testing SVG Wireframe Generation...")
    print("=" * 50)
    
    # Test main SVG generation
    test1_passed = test_svg_generation()
    
    # Test fallback generation
    test2_passed = test_fallback_spec()
    
    print("=" * 50)
    if test1_passed and test2_passed:
        print("🎉 All tests passed! SVG generation is working correctly.")
    else:
        print("❌ Some tests failed. Check the output above for details.")
    
    print("\n📁 Generated files:")
    if os.path.exists('test_wireframe.svg'):
        print("  - test_wireframe.svg (main test)")
    if os.path.exists('test_fallback.svg'):
        print("  - test_fallback.svg (fallback test)")
