"""
Demo script showing custom task status creation functionality
"""
import requests
import json

BASE_URL = "http://localhost:8000"

def login():
    """Login and get access token"""
    response = requests.post(
        f"{BASE_URL}/auth/login",
        json={"email": "admin@vfx.com", "password": "admin123"}
    )
    return response.json()["access_token"] if response.status_code == 200 else None

def demo():
    print("=" * 70)
    print("CUSTOM TASK STATUS CREATION - DEMO")
    print("=" * 70)
    
    token = login()
    if not token:
        print("❌ Login failed")
        return
    
    headers = {"Authorization": f"Bearer {token}"}
    project_id = 1
    
    print("\n✅ Successfully logged in as admin")
    
    # Get initial state
    print("\n" + "-" * 70)
    print("STEP 1: Get current task statuses")
    print("-" * 70)
    response = requests.get(
        f"{BASE_URL}/projects/{project_id}/task-statuses",
        headers=headers
    )
    if response.status_code == 200:
        data = response.json()
        print(f"System statuses: {len(data['system_statuses'])}")
        print(f"Custom statuses: {len(data['statuses'])}")
        initial_count = len(data['statuses'])
    
    # Create status with auto-assigned color
    print("\n" + "-" * 70)
    print("STEP 2: Create status with auto-assigned color")
    print("-" * 70)
    print("Request: POST /projects/1/task-statuses")
    print('Payload: {"name": "Needs Feedback"}')
    
    response = requests.post(
        f"{BASE_URL}/projects/{project_id}/task-statuses",
        headers=headers,
        json={"name": "Needs Feedback"}
    )
    
    if response.status_code == 201:
        data = response.json()
        print(f"\n✅ Status created successfully!")
        print(f"   ID: {data['status']['id']}")
        print(f"   Name: {data['status']['name']}")
        print(f"   Color: {data['status']['color']} (auto-assigned)")
        print(f"   Order: {data['status']['order']}")
    else:
        print(f"❌ Failed: {response.status_code}")
        print(f"   {response.json()}")
    
    # Create status with custom color
    print("\n" + "-" * 70)
    print("STEP 3: Create status with custom color")
    print("-" * 70)
    print("Request: POST /projects/1/task-statuses")
    print('Payload: {"name": "Client Review", "color": "#FF6B6B"}')
    
    response = requests.post(
        f"{BASE_URL}/projects/{project_id}/task-statuses",
        headers=headers,
        json={"name": "Client Review", "color": "#FF6B6B"}
    )
    
    if response.status_code == 201:
        data = response.json()
        print(f"\n✅ Status created successfully!")
        print(f"   ID: {data['status']['id']}")
        print(f"   Name: {data['status']['name']}")
        print(f"   Color: {data['status']['color']} (custom)")
        print(f"   Order: {data['status']['order']}")
    else:
        print(f"❌ Failed: {response.status_code}")
        print(f"   {response.json()}")
    
    # Try to create duplicate
    print("\n" + "-" * 70)
    print("STEP 4: Try to create duplicate (should fail)")
    print("-" * 70)
    print("Request: POST /projects/1/task-statuses")
    print('Payload: {"name": "Needs Feedback"}')
    
    response = requests.post(
        f"{BASE_URL}/projects/{project_id}/task-statuses",
        headers=headers,
        json={"name": "Needs Feedback"}
    )
    
    if response.status_code == 409:
        print(f"\n✅ Correctly rejected duplicate!")
        print(f"   Status: {response.status_code} Conflict")
        print(f"   Message: {response.json()['detail']}")
    else:
        print(f"❌ Unexpected response: {response.status_code}")
    
    # Get final state
    print("\n" + "-" * 70)
    print("STEP 5: Get updated task statuses")
    print("-" * 70)
    response = requests.get(
        f"{BASE_URL}/projects/{project_id}/task-statuses",
        headers=headers
    )
    if response.status_code == 200:
        data = response.json()
        print(f"System statuses: {len(data['system_statuses'])}")
        print(f"Custom statuses: {len(data['statuses'])}")
        print(f"\nAll custom statuses:")
        for status in data['statuses']:
            default_marker = " (DEFAULT)" if status['is_default'] else ""
            print(f"  • {status['name']}{default_marker}")
            print(f"    ID: {status['id']}, Color: {status['color']}, Order: {status['order']}")
    
    print("\n" + "=" * 70)
    print("DEMO COMPLETE")
    print("=" * 70)

if __name__ == "__main__":
    demo()
