#!/usr/bin/env python3

import sys
import os
sys.path.insert(0, '/app')

from src.api.database import MusicBrainzDatabase

def debug_collaboration():
    """Debug the collaboration parsing and database queries."""
    
    db = MusicBrainzDatabase()
    if not db.connect():
        print("❌ Failed to connect to database")
        return
    
    print("✅ Connected to database")
    
    # Test cases that are failing
    test_cases = [
        ("Pitbull ft. Ne-Yo, Afrojack & Nayer", "Give Me Everything"),
        ("Aretha Franklin ft. G. Michael", "I Knew You Were Waiting For Me"),
    ]
    
    for artist_name, title in test_cases:
        print(f"\n🔍 Testing: {artist_name} - {title}")
        print("=" * 60)
        
        # Test collaboration parsing
        main_artist, collaborators = db._parse_complex_collaboration(artist_name)
        print(f"Main artist: '{main_artist}'")
        print(f"Collaborators: {collaborators}")
        
        # Test artist credit finding
        result = db.find_artist_credit(artist_name, title)
        if result:
            artist_credit_id, artist_string, recording_mbid = result
            print(f"✅ Found artist credit: {artist_string}")
            print(f"Recording MBID: {recording_mbid}")
        else:
            print("❌ No artist credit found")
            
            # Try to find the recording directly
            print("\n🔍 Trying to find recording directly...")
            recording_result = db.fuzzy_search_recording(title)
            if recording_result:
                recording_name, recording_mbid, score = recording_result
                print(f"Found recording: {recording_name} (score: {score})")
                
                # Get recording info
                recording_info = db.get_recording_info(recording_mbid)
                if recording_info:
                    print(f"Recording artist: {recording_info.get('artist_credit', 'Unknown')}")
            else:
                print("❌ No recording found")

if __name__ == "__main__":
    debug_collaboration() 