#!/usr/bin/env python3

import requests

def test_photo_access():
    """Test if restaurant photos can be accessed via web server"""
    
    # First get a restaurant with photo
    print("🔍 Getting restaurant with photo...")
    try:
        response = requests.get("http://localhost:8001/api/restaurants?per_page=1")
        if response.status_code == 200:
            data = response.json()
            if data.get('restaurants') and len(data['restaurants']) > 0:
                restaurant = data['restaurants'][0]
                photo_url = restaurant.get('image')
                print(f"📋 Restaurant: {restaurant.get('name')}")
                print(f"🖼️ Photo URL: {photo_url}")
                
                if photo_url and photo_url.startswith('/restaurant_photos/'):
                    # Test photo access
                    full_url = f"http://localhost:8001{photo_url}"
                    print(f"\n🌐 Testing photo access: {full_url}")
                    
                    photo_response = requests.head(full_url)
                    print(f"📊 Status Code: {photo_response.status_code}")
                    if photo_response.status_code == 200:
                        print("✅ Photo accessible!")
                        headers = dict(photo_response.headers)
                        print(f"📝 Content-Type: {headers.get('Content-Type', 'unknown')}")
                        print(f"📏 Content-Length: {headers.get('Content-Length', 'unknown')} bytes")
                    else:
                        print("❌ Photo not accessible")
                        print(f"Error: {photo_response.text}")
                else:
                    print("❌ Restaurant has no photo or uses default image")
            else:
                print("❌ No restaurants found")
        else:
            print(f"❌ API request failed: {response.status_code}")
    except Exception as e:
        print(f"❌ Error: {e}")

if __name__ == "__main__":
    test_photo_access()