#!/usr/bin/env python3
"""
Test the frontend API connection through the proxy.
"""

import requests
import json

FRONTEND_URL = "http://localhost:5174"

def test_frontend_api():
    """Test the frontend API connection through Vite proxy."""
    
    print("Testing Frontend API Connection")
    print("=" * 40)
    
    # Test 1: Login through frontend proxy
    print("1. Testing login through frontend proxy...")
    login_data = {
        "email": "admin@vfx.com",
        "password": "admin123"
    }
    
    try:
        response = requests.post(f"{FRONTEND_URL}/api/auth/login", json=login_data)
        if response.status_code == 200:
            print("   ✅ Login through proxy successful")
            auth_data = response.json()
            access_token = auth_data.get("access_token")
            print(f"   Token received: {access_token[:20]}...")
        else:
            print(f"   ❌ Login through proxy failed: {response.status_code}")
            print(f"   Response: {response.text}")
            return False
    except Exception as e:
        print(f"   ❌ Login through proxy error: {e}")
        return False
    
    # Test 2: Get projects through frontend proxy
    print("\n2. Testing get projects through frontend proxy...")
    headers = {"Authorization": f"Bearer {access_token}"}
    
    try:
        response = requests.get(f"{FRONTEND_URL}/api/projects/", headers=headers)
        if response.status_code == 200:
            print("   ✅ Get projects through proxy successful")
            projects = response.json()
            print(f"   Found {len(projects)} projects:")
            for project in projects:
                print(f"     - {project.get('name')} ({project.get('code_name')})")
        else:
            print(f"   ❌ Get projects through proxy failed: {response.status_code}")
            print(f"   Response: {response.text}")
            return False
    except Exception as e:
        print(f"   ❌ Get projects through proxy error: {e}")
        return False
    
    print("\n✅ Frontend API connection working!")
    print("\nThe frontend should now be able to:")
    print("- Login with admin@vfx.com / admin123")
    print("- Load and display projects correctly")
    print("- Navigate to project management features")
    
    return True

if __name__ == "__main__":
    test_frontend_api()