#!/usr/bin/env python3
import re

def parse_complex_collaboration(artist_string):
    """
    Parse complex collaboration strings like "Pitbull ft. Ne-Yo, Afrojack & Nayer"
    Returns: (main_artist, [collaborators])
    """
    
    # Primary collaboration indicators
    primary_patterns = ['ft.', 'feat.', 'featuring']
    
    # Check if this is a collaboration
    is_collaboration = False
    split_pattern = None
    
    for pattern in primary_patterns:
        if pattern in artist_string.lower():
            is_collaboration = True
            split_pattern = pattern
            break
    
    if not is_collaboration:
        return (artist_string, [])
    
    # Split on the primary pattern
    parts = artist_string.split(split_pattern)
    if len(parts) < 2:
        return (artist_string, [])
    
    main_artist = parts[0].strip()
    collaborators_string = split_pattern.join(parts[1:]).strip()
    
    # Now parse the collaborators string which might have multiple separators
    collaborators = parse_collaborators(collaborators_string)
    
    return (main_artist, collaborators)

def parse_collaborators(collaborators_string):
    """
    Parse a string like "Ne-Yo, Afrojack & Nayer" into individual artists
    """
    if not collaborators_string:
        return []
    
    # Split on common separators: comma, ampersand, "and"
    # Use regex to handle multiple separators
    separators = r'[,&]|\band\b'
    
    # Split and clean up each part
    parts = re.split(separators, collaborators_string, flags=re.IGNORECASE)
    
    collaborators = []
    for part in parts:
        part = part.strip()
        if part:  # Skip empty parts
            collaborators.append(part)
    
    return collaborators

def test_complex_collaborations():
    """Test complex collaboration parsing."""
    
    test_cases = [
        "Pitbull ft. Ne-Yo, Afrojack & Nayer",
        "Cardi B ft. Bad Bunny, J Balvin",
        "Shawn Mendes ft. Camila Cabello",
        "Rihanna ft. Drake",
        "G-Eazy & Halsey",
        "Simon & Garfunkel",  # Not a collaboration
        "Hall & Oates",       # Not a collaboration
        "P!nk ft. Nate Ruess",
        "Maroon 5 ft. Cardi B",
        "Ariana Grande ft. Big Sean",
        "Clean Bandit ft. Jess Glynne",
        "The Weeknd ft. Daft Punk",
        "Wiz Khalifa ft. Charlie Puth",
        "Blink-182",  # Not a collaboration
        "U2",  # Not a collaboration
    ]
    
    print("Testing complex collaboration parsing:")
    print("=" * 60)
    
    for artist_string in test_cases:
        main_artist, collaborators = parse_complex_collaboration(artist_string)
        
        if collaborators:
            print(f"✅ COLLABORATION: '{artist_string}'")
            print(f"   Main artist: '{main_artist}'")
            print(f"   Collaborators: {collaborators}")
            print(f"   Total artists: {len(collaborators) + 1}")
        else:
            print(f"❌ NOT COLLABORATION: '{artist_string}'")
        print()

if __name__ == "__main__":
    test_complex_collaborations() 