"""
Test script for project settings endpoints
"""

import requests
import json

BASE_URL = "http://localhost:8000"

def test_project_settings():
    """Test project settings endpoints"""
    
    # First, login to get a token
    print("1. Logging in...")
    login_response = requests.post(
        f"{BASE_URL}/auth/login",
        data={
            "username": "admin@example.com",
            "password": "admin123"
        }
    )
    
    if login_response.status_code != 200:
        print(f"❌ Login failed: {login_response.status_code}")
        print(login_response.text)
        return
    
    token = login_response.json()["access_token"]
    headers = {"Authorization": f"Bearer {token}"}
    print("✓ Login successful")
    
    # Get projects
    print("\n2. Getting projects...")
    projects_response = requests.get(f"{BASE_URL}/projects/", headers=headers)
    
    if projects_response.status_code != 200:
        print(f"❌ Failed to get projects: {projects_response.status_code}")
        return
    
    projects = projects_response.json()
    if not projects:
        print("❌ No projects found")
        return
    
    project_id = projects[0]["id"]
    print(f"✓ Found project: {projects[0]['name']} (ID: {project_id})")
    
    # Get project settings
    print("\n3. Getting project settings...")
    settings_response = requests.get(
        f"{BASE_URL}/projects/{project_id}/settings",
        headers=headers
    )
    
    if settings_response.status_code != 200:
        print(f"❌ Failed to get settings: {settings_response.status_code}")
        print(settings_response.text)
        return
    
    settings = settings_response.json()
    print("✓ Current settings:")
    print(json.dumps(settings, indent=2))
    
    # Update project settings
    print("\n4. Updating project settings...")
    new_settings = {
        "upload_data_location": "/mnt/projects/test-project/uploads",
        "asset_task_templates": {
            "characters": ["modeling", "surfacing", "rigging"],
            "props": ["modeling", "surfacing"],
            "sets": ["modeling", "surfacing"],
            "vehicles": ["modeling", "surfacing", "rigging"]
        },
        "shot_task_templates": ["layout", "animation", "lighting", "compositing"]
    }
    
    update_response = requests.put(
        f"{BASE_URL}/projects/{project_id}/settings",
        headers=headers,
        json=new_settings
    )
    
    if update_response.status_code != 200:
        print(f"❌ Failed to update settings: {update_response.status_code}")
        print(update_response.text)
        return
    
    updated_settings = update_response.json()
    print("✓ Settings updated:")
    print(json.dumps(updated_settings, indent=2))
    
    # Verify the update
    print("\n5. Verifying update...")
    verify_response = requests.get(
        f"{BASE_URL}/projects/{project_id}/settings",
        headers=headers
    )
    
    if verify_response.status_code != 200:
        print(f"❌ Failed to verify: {verify_response.status_code}")
        return
    
    verified_settings = verify_response.json()
    print("✓ Verified settings:")
    print(json.dumps(verified_settings, indent=2))
    
    print("\n✅ All tests passed!")

if __name__ == "__main__":
    print("Testing Project Settings API")
    print("=" * 50)
    test_project_settings()
