"""Test editing a user's name"""
import requests

BASE_URL = "http://localhost:8000"

def test_edit_user_name():
    # Login as admin
    login_response = requests.post(
        f"{BASE_URL}/auth/login",
        json={
            "email": "admin@vfx.com",
            "password": "admin123"
        }
    )
    
    if login_response.status_code != 200:
        print(f"Login failed: {login_response.status_code}")
        print(login_response.json())
        return
    
    token = login_response.json()["access_token"]
    headers = {"Authorization": f"Bearer {token}"}
    
    # Get user 5's current data
    user_id = 5
    get_response = requests.get(
        f"{BASE_URL}/users/{user_id}",
        headers=headers
    )
    
    print(f"\nCurrent user data:")
    print(f"Status: {get_response.status_code}")
    if get_response.status_code == 200:
        user_data = get_response.json()
        print(f"Name: {user_data['first_name']} {user_data['last_name']}")
        print(f"Email: {user_data['email']}")
    
    # Edit the user's name
    edit_data = {
        "first_name": "UpdatedFirst",
        "last_name": "UpdatedLast",
        "email": user_data['email'],
        "role": user_data['role'],
        "is_approved": user_data['is_approved'],
        "is_admin": user_data['is_admin']
    }
    
    edit_response = requests.put(
        f"{BASE_URL}/users/{user_id}",
        json=edit_data,
        headers=headers
    )
    
    print(f"\nEdit response:")
    print(f"Status: {edit_response.status_code}")
    print(f"Response: {edit_response.json()}")
    
    if edit_response.status_code == 200:
        updated_user = edit_response.json()
        print(f"\nUpdated name: {updated_user['first_name']} {updated_user['last_name']}")

if __name__ == "__main__":
    test_edit_user_name()
