#!/usr/bin/env python3
"""
Manual test for form validation errors showing as SweetAlert toasts.
"""

import requests
from bs4 import BeautifulSoup

def test_login_error():
    """Test a simple login error scenario"""
    session = requests.Session()
    login_url = "http://localhost:8090/auth/login"
    
    print("🧪 Testing Form Validation Errors with SweetAlert")
    print("=" * 60)
    
    # Get the login page first
    response = session.get(login_url)
    if response.status_code != 200:
        print(f"❌ Could not access login page: {response.status_code}")
        return
    
    # Extract CSRF token
    soup = BeautifulSoup(response.content, 'html.parser')
    csrf_token = soup.find('input', {'name': 'csrfmiddlewaretoken'})
    
    if not csrf_token:
        print("❌ Could not find CSRF token")
        return
    
    csrf_value = csrf_token.get('value')
    print(f"✅ Got CSRF token: {csrf_value[:10]}...")
    
    # Test with invalid credentials
    print("\n🔍 Testing with invalid credentials...")
    login_data = {
        'email': 'invalid@example.com',
        'password': 'wrongpassword',
        'csrfmiddlewaretoken': csrf_value
    }
    
    response = session.post(login_url, data=login_data, allow_redirects=False)
    print(f"Response status: {response.status_code}")
    
    if response.status_code == 200:
        content = response.text
        
        # Check for messages in response
        if 'messages' in content and 'Swal.fire' in content:
            print("✅ SweetAlert integration found in response!")
            
            # Check for specific error patterns
            if 'Invalid email or password' in content or 'error' in content:
                print("✅ Error message should display as SweetAlert toast!")
            else:
                print("❓ Error message content may need verification")
        else:
            print("❌ SweetAlert integration may be missing")
    else:
        print(f"❓ Unexpected response: {response.status_code}")
    
    print("\n📋 Manual Testing Instructions:")
    print("1. Open http://localhost:8090/auth/login in your browser")
    print("2. Leave email field empty and click Login")
    print("3. You should see an error toast in the top-right corner")
    print("4. Try entering 'invalid-email' (without @) and click Login")
    print("5. You should see an email format error toast")
    print("6. Try entering 'wrong@email.com' with password 'wrong123'")
    print("7. You should see an authentication error toast")
    print("8. All errors should appear as SweetAlert toasts, not as HTML text!")

if __name__ == "__main__":
    test_login_error()
