"""Tests for flexible result comparison (tuples vs lists, multiple valid answers)."""

import pytest


def test_tuple_equals_list(api_client, valid_token):
    """Test that returning a tuple is accepted as equal to a list."""
    code = '''def two_sum(nums, target):
    """Solution that returns tuple instead of list."""
    seen = {}
    for i, num in enumerate(nums):
        complement = target - num
        if complement in seen:
            return (seen[complement], i)  # Tuple, not list!
        seen[num] = i
    return ()
'''
    
    response = api_client['submit'](
        code=code,
        function_name='two_sum',
        token=valid_token
    )
    
    assert response.status_code == 200
    data = response.json()
    
    # Should pass despite returning tuples
    assert data['success'] is True
    assert data['passed'] == 7


def test_multiple_valid_answers_accepted(api_client, valid_token):
    """Test that any valid answer from multiple options is accepted."""
    # The test cases for Two Sum accept [0,1] or [1,0]
    # Let's return [1,0] which should be accepted
    code = '''def two_sum(nums, target):
    """Solution that returns indices in reverse order."""
    seen = {}
    for i, num in enumerate(nums):
        complement = target - num
        if complement in seen:
            # Return in reverse order
            return [i, seen[complement]]
        seen[num] = i
    return []
'''
    
    response = api_client['submit'](
        code=code,
        function_name='two_sum',
        token=valid_token
    )
    
    assert response.status_code == 200
    data = response.json()
    
    # Should pass with reversed indices (both orders are valid)
    # Note: This might fail if not all test cases accept both orders
    # But at least test 1 should accept it
    assert data['passed'] >= 1


def test_nested_tuple_to_list_conversion(api_client, valid_token):
    """Test that nested tuples are converted to lists recursively."""
    # Create a question that might return nested structures
    code = '''def two_sum(nums, target):
    """Solution returning nested tuple."""
    seen = {}
    for i, num in enumerate(nums):
        complement = target - num
        if complement in seen:
            # Return as nested tuple (though Two Sum doesn't need this)
            result = (seen[complement], i)
            return list(result)  # Convert explicitly for now
        seen[num] = i
    return []
'''
    
    response = api_client['submit'](
        code=code,
        function_name='two_sum',
        token=valid_token
    )
    
    assert response.status_code == 200
    data = response.json()
    
    assert data['success'] is True


def test_empty_result_handling(api_client, valid_token):
    """Test that empty results are handled correctly."""
    code = '''def two_sum(nums, target):
    """Solution that always returns empty."""
    return []
'''
    
    response = api_client['submit'](
        code=code,
        function_name='two_sum',
        token=valid_token
    )
    
    assert response.status_code == 200
    data = response.json()
    
    # Should fail all tests (no valid answer is empty list)
    assert data['success'] is False
    assert data['passed'] == 0


def test_wrong_type_result(api_client, valid_token):
    """Test that returning wrong type is handled gracefully."""
    code = '''def two_sum(nums, target):
    """Solution that returns wrong type."""
    return "not a list"
'''
    
    response = api_client['submit'](
        code=code,
        function_name='two_sum',
        token=valid_token
    )
    
    assert response.status_code == 200
    data = response.json()
    
    # Should fail all tests
    assert data['success'] is False


def test_none_result_handling(api_client, valid_token):
    """Test that returning None is handled correctly."""
    code = '''def two_sum(nums, target):
    """Solution that returns None."""
    return None
'''
    
    response = api_client['submit'](
        code=code,
        function_name='two_sum',
        token=valid_token
    )
    
    assert response.status_code == 200
    data = response.json()
    
    # Should fail all tests
    assert data['success'] is False
    assert data['passed'] == 0
