#!/usr/bin/env python3

import requests
import json

# Configuration
BASE_URL = "http://localhost:8000"
LOGIN_URL = f"{BASE_URL}/auth/login"
ASSETS_URL = f"{BASE_URL}/assets/"

def test_asset_api():
    print("Testing Asset API with Task Status")
    print("=" * 50)
    
    # Login first
    print("1. Logging in...")
    login_data = {
        "email": "admin@vfx.com",
        "password": "admin123"
    }
    
    response = requests.post(LOGIN_URL, json=login_data)
    if response.status_code != 200:
        print(f"❌ Login failed: {response.status_code}")
        print(response.text)
        return
    
    token_data = response.json()
    token = token_data["access_token"]
    headers = {"Authorization": f"Bearer {token}"}
    print("✅ Login successful")
    
    # Get projects first
    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"✅ Using project ID: {project_id}")
    
    # Test asset listing with task status
    print("\n3. Testing asset listing with task status...")
    params = {"project_id": project_id}
    response = requests.get(ASSETS_URL, params=params, headers=headers)
    
    if response.status_code != 200:
        print(f"❌ Failed to get assets: {response.status_code}")
        print(response.text)
        return
    
    assets = response.json()
    print(f"✅ Found {len(assets)} assets")
    
    if assets:
        asset = assets[0]
        print(f"   Asset: {asset['name']}")
        print(f"   Category: {asset['category']}")
        print(f"   Task count: {asset.get('task_count', 0)}")
        print(f"   Task status: {asset.get('task_status', {})}")
        print(f"   Task details: {len(asset.get('task_details', []))} tasks")
    
    # Test sorting by task status
    print("\n4. Testing sorting by task status...")
    params = {
        "project_id": project_id,
        "sort_by": "modeling_status",
        "sort_direction": "asc"
    }
    response = requests.get(ASSETS_URL, params=params, headers=headers)
    
    if response.status_code == 200:
        print("✅ Task status sorting works")
    else:
        print(f"❌ Task status sorting failed: {response.status_code}")
    
    # Test filtering by task status
    print("\n5. Testing filtering by task status...")
    params = {
        "project_id": project_id,
        "task_status_filter": "modeling:not_started"
    }
    response = requests.get(ASSETS_URL, params=params, headers=headers)
    
    if response.status_code == 200:
        filtered_assets = response.json()
        print(f"✅ Task status filtering works - found {len(filtered_assets)} assets")
    else:
        print(f"❌ Task status filtering failed: {response.status_code}")
    
    print("\n✅ Asset API tests completed!")

if __name__ == "__main__":
    test_asset_api()