"""
Test script for task status endpoints
"""
import requests
import json

BASE_URL = "http://localhost:8000"

def test_get_all_task_statuses():
    """Test getting all task statuses for a project"""
    
    # First, login to get a token
    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.text)
        return
    
    token = login_response.json()["access_token"]
    headers = {"Authorization": f"Bearer {token}"}
    
    print("✅ Login successful")
    
    # Get list of 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}")
        print(projects_response.text)
        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 GET /projects/{project_id}/task-statuses
    print("\n--- Testing GET /projects/{project_id}/task-statuses ---")
    response = requests.get(
        f"{BASE_URL}/projects/{project_id}/task-statuses",
        headers=headers
    )
    
    print(f"Status Code: {response.status_code}")
    
    if response.status_code == 200:
        data = response.json()
        print("✅ Successfully retrieved task statuses")
        print(f"\nResponse:")
        print(json.dumps(data, indent=2))
        
        # Validate response structure
        assert "statuses" in data, "Missing 'statuses' field"
        assert "system_statuses" in data, "Missing 'system_statuses' field"
        assert "default_status_id" in data, "Missing 'default_status_id' field"
        
        print(f"\n✅ System statuses count: {len(data['system_statuses'])}")
        print(f"✅ Custom statuses count: {len(data['statuses'])}")
        print(f"✅ Default status ID: {data['default_status_id']}")
        
        # Verify system statuses
        expected_system_statuses = ["not_started", "in_progress", "submitted", "approved", "retake"]
        system_status_ids = [s["id"] for s in data["system_statuses"]]
        
        for expected_id in expected_system_statuses:
            if expected_id in system_status_ids:
                print(f"✅ System status '{expected_id}' found")
            else:
                print(f"❌ System status '{expected_id}' missing")
        
        # Verify all system statuses have required fields
        for status in data["system_statuses"]:
            assert "id" in status, f"System status missing 'id': {status}"
            assert "name" in status, f"System status missing 'name': {status}"
            assert "color" in status, f"System status missing 'color': {status}"
            assert "is_system" in status, f"System status missing 'is_system': {status}"
            assert status["is_system"] == True, f"System status 'is_system' should be True: {status}"
        
        print("\n✅ All system statuses have required fields")
        
    else:
        print(f"❌ Failed to get task statuses")
        print(response.text)


if __name__ == "__main__":
    test_get_all_task_statuses()
