#!/usr/bin/env python3
"""
Check how MusicBrainz stores collaboration information.
"""

import psycopg2
import psycopg2.extras

# Database configuration
DB_HOST = "172.18.0.2"
DB_PORT = 5432
DB_NAME = "musicbrainz_db"
DB_USER = "musicbrainz"
DB_PASSWORD = "musicbrainz"

def check_collaboration():
    """Check how Afrojack and Chris Brown collaboration is stored."""
    try:
        connection = psycopg2.connect(
            host=DB_HOST,
            port=DB_PORT,
            database=DB_NAME,
            user=DB_USER,
            password=DB_PASSWORD,
            connect_timeout=10
        )
        cursor = connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        
        print("🔍 Checking Afrojack and Chris Brown collaboration...")
        print("=" * 60)
        
        # Find Afrojack
        cursor.execute("SELECT id, name, gid FROM artist WHERE name = 'Afrojack' LIMIT 1")
        afrojack = cursor.fetchone()
        
        # Find Chris Brown
        cursor.execute("SELECT id, name, gid FROM artist WHERE name = 'Chris Brown' LIMIT 1")
        chris_brown = cursor.fetchone()
        
        if afrojack and chris_brown:
            print(f"✅ Found Afrojack: {afrojack['name']} (ID: {afrojack['id']}, MBID: {afrojack['gid']})")
            print(f"✅ Found Chris Brown: {chris_brown['name']} (ID: {chris_brown['id']}, MBID: {chris_brown['gid']})")
            
            # Look for recordings with both artists
            query = """
                SELECT DISTINCT r.name as recording_name, r.gid as recording_mbid, ac.id as artist_credit_id
                FROM recording r
                JOIN artist_credit ac ON r.artist_credit = ac.id
                JOIN artist_credit_name acn1 ON ac.id = acn1.artist_credit
                JOIN artist_credit_name acn2 ON ac.id = acn2.artist_credit
                JOIN artist a1 ON acn1.artist = a1.id
                JOIN artist a2 ON acn2.artist = a2.id
                WHERE a1.name = 'Afrojack' 
                AND a2.name = 'Chris Brown'
                AND r.name ILIKE '%As Your Friend%'
                LIMIT 5
            """
            cursor.execute(query)
            collaborations = cursor.fetchall()
            
            print(f"\n🎵 Found {len(collaborations)} collaborations:")
            for collab in collaborations:
                print(f"  Recording: {collab['recording_name']}")
                print(f"  Recording MBID: {collab['recording_mbid']}")
                print(f"  Artist Credit ID: {collab['artist_credit_id']}")
                
                # Get all artists in this collaboration
                artist_query = """
                    SELECT a.name, acn.join_phrase, acn.position
                    FROM artist_credit_name acn
                    JOIN artist a ON acn.artist = a.id
                    WHERE acn.artist_credit = %s
                    ORDER BY acn.position
                """
                cursor.execute(artist_query, (collab['artist_credit_id'],))
                artists = cursor.fetchall()
                
                print("  Artists:")
                for artist in artists:
                    join_phrase = artist['join_phrase'] if artist['join_phrase'] else ""
                    print(f"    {artist['name']}{join_phrase}")
                print()
        
        cursor.close()
        connection.close()
        
    except Exception as e:
        print(f"❌ Error: {e}")

if __name__ == "__main__":
    check_collaboration() 