"""
Test edge cases for bulk shot creation project validation
"""
import requests
import json

BASE_URL = "http://localhost:8000"

# Test credentials
LOGIN_DATA = {
    "email": "admin@vfx.com",
    "password": "admin123"
}


def login():
    """Login and get access token"""
    response = requests.post(f"{BASE_URL}/auth/login", json=LOGIN_DATA)
    if response.status_code == 200:
        return response.json()["access_token"]
    else:
        print(f"Login failed: {response.status_code}")
        return None


def test_bulk_shot_edge_cases():
    """Test edge cases for bulk shot creation"""
    print("=== Test: Bulk Shot Creation Edge Cases ===\n")
    
    token = login()
    if not token:
        return
    
    headers = {"Authorization": f"Bearer {token}"}
    episode_id = 1
    
    # Test 1: Large bulk creation
    print("1. Testing large bulk creation (50 shots)...")
    import time
    timestamp = int(time.time())
    
    large_bulk_data = {
        "name_prefix": f"LARGE_{timestamp}_",
        "shot_count": 50,
        "start_number": 1,
        "number_padding": 3,
        "frame_start": 1001,
        "frame_end": 1100,
        "description_template": "Large bulk test shot {shot_name}",
        "create_default_tasks": True
    }
    
    response = requests.post(
        f"{BASE_URL}/shots/bulk?episode_id={episode_id}",
        headers=headers,
        json=large_bulk_data
    )
    
    print(f"   Status: {response.status_code}")
    if response.status_code == 201:
        result = response.json()
        print(f"   ✓ Success: Created {len(result['created_shots'])} shots")
        print(f"   ✓ All same project: {len(set(shot['project_id'] for shot in result['created_shots'])) == 1}")
        print(f"   ✓ Total tasks: {result['created_tasks_count']}")
    else:
        print(f"   ✗ Error: {response.json()}")
    
    # Test 2: Zero padding edge case
    print("\n2. Testing zero padding...")
    zero_pad_data = {
        "name_prefix": f"ZERO_{timestamp}_",
        "shot_count": 3,
        "start_number": 1,
        "number_padding": 1,  # Minimal padding
        "frame_start": 1001,
        "frame_end": 1100,
        "create_default_tasks": False
    }
    
    response = requests.post(
        f"{BASE_URL}/shots/bulk?episode_id={episode_id}",
        headers=headers,
        json=zero_pad_data
    )
    
    print(f"   Status: {response.status_code}")
    if response.status_code == 201:
        result = response.json()
        shot_names = [shot['name'] for shot in result['created_shots']]
        print(f"   ✓ Success: {shot_names}")
        print(f"   ✓ Correct naming: {all('_' + str(i+1) in name for i, name in enumerate(shot_names))}")
    else:
        print(f"   ✗ Error: {response.json()}")
    
    # Test 3: High start number
    print("\n3. Testing high start number...")
    high_start_data = {
        "name_prefix": f"HIGH_{timestamp}_",
        "shot_count": 3,
        "start_number": 9998,  # High number
        "number_padding": 4,
        "frame_start": 1001,
        "frame_end": 1100,
        "create_default_tasks": False
    }
    
    response = requests.post(
        f"{BASE_URL}/shots/bulk?episode_id={episode_id}",
        headers=headers,
        json=high_start_data
    )
    
    print(f"   Status: {response.status_code}")
    if response.status_code == 201:
        result = response.json()
        shot_names = [shot['name'] for shot in result['created_shots']]
        print(f"   ✓ Success: {shot_names}")
        expected_names = [f"HIGH_{timestamp}_9998", f"HIGH_{timestamp}_9999", f"HIGH_{timestamp}_{10000:04d}"]
        print(f"   ✓ Expected names match: {shot_names == expected_names}")
    else:
        print(f"   ✗ Error: {response.json()}")
    
    # Test 4: Invalid episode ID
    print("\n4. Testing invalid episode ID...")
    invalid_episode_data = {
        "name_prefix": f"INVALID_{timestamp}_",
        "shot_count": 1,
        "start_number": 1,
        "number_padding": 3,
        "frame_start": 1001,
        "frame_end": 1100,
        "create_default_tasks": False
    }
    
    response = requests.post(
        f"{BASE_URL}/shots/bulk?episode_id=99999",  # Non-existent episode
        headers=headers,
        json=invalid_episode_data
    )
    
    print(f"   Status: {response.status_code}")
    if response.status_code == 404:
        print(f"   ✓ Correctly rejected invalid episode: {response.json()['detail']}")
    else:
        print(f"   ✗ Should have rejected invalid episode: {response.json()}")
    
    # Test 5: Empty prefix
    print("\n5. Testing empty prefix...")
    empty_prefix_data = {
        "name_prefix": "",  # Empty prefix
        "shot_count": 1,
        "start_number": 1,
        "number_padding": 3,
        "frame_start": 1001,
        "frame_end": 1100,
        "create_default_tasks": False
    }
    
    response = requests.post(
        f"{BASE_URL}/shots/bulk?episode_id={episode_id}",
        headers=headers,
        json=empty_prefix_data
    )
    
    print(f"   Status: {response.status_code}")
    if response.status_code == 422:  # Validation error
        print(f"   ✓ Correctly rejected empty prefix")
    else:
        print(f"   ✗ Should have rejected empty prefix: {response.json()}")
    
    print("\n=== Edge Cases Test Completed ===")


if __name__ == "__main__":
    test_bulk_shot_edge_cases()