#!/usr/bin/env python3
"""
Test API Endpoints
This script tests the website API endpoints to ensure they're working correctly.
"""

import requests
import json
import os

def test_api_endpoints():
    """Test the website API endpoints"""
    print("🧪 Testing Website API Endpoints")
    print("=" * 50)
    
    base_url = "http://localhost:5008"
    
    # Test 1: Check if website is running
    try:
        response = requests.get(f"{base_url}/", timeout=5)
        if response.status_code == 200:
            print("✅ Website is running")
        else:
            print(f"❌ Website returned status {response.status_code}")
            return
    except requests.exceptions.RequestException as e:
        print(f"❌ Website is not running: {e}")
        print("Please start the website with: python app.py")
        return
    
    # Test 2: Test list-codegen-files endpoint without auth
    try:
        response = requests.get(f"{base_url}/api/list-codegen-files", timeout=5)
        print(f"📊 list-codegen-files without auth: {response.status_code}")
        if response.status_code == 401:
            print("✅ Endpoint correctly requires authentication")
        else:
            print(f"⚠️ Unexpected response: {response.text[:100]}")
    except requests.exceptions.RequestException as e:
        print(f"❌ Error testing list-codegen-files: {e}")
    
    # Test 3: Test with a mock JWT token
    try:
        # Create a mock JWT token (this won't work but will show the response)
        mock_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiMzRkMGYxMzItNzZmZi00YWRmLWI5OGEtZTg2NDkwMTZlNzQ3In0.mock"
        headers = {"Authorization": f"Bearer {mock_token}"}
        response = requests.get(f"{base_url}/api/list-codegen-files", headers=headers, timeout=5)
        print(f"📊 list-codegen-files with mock token: {response.status_code}")
        if response.status_code == 401:
            print("✅ Endpoint correctly rejects invalid token")
        else:
            print(f"⚠️ Unexpected response: {response.text[:100]}")
    except requests.exceptions.RequestException as e:
        print(f"❌ Error testing with mock token: {e}")
    
    print("\n💡 To test with real authentication:")
    print("1. Open the website in your browser")
    print("2. Log in to get a real JWT token")
    print("3. Open browser Developer Tools (F12)")
    print("4. Go to Application/Storage tab")
    print("5. Look for 'authToken' in localStorage")
    print("6. Copy the token and test the API manually")

if __name__ == "__main__":
    test_api_endpoints()
