#!/usr/bin/env python3
"""
Simple debug script to test database queries directly.
"""

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 test_database_queries():
    """Test database queries directly to find the tuple error."""
    print("🔍 Testing database queries directly...")
    
    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("✅ Connected to database")
        
        # Test 1: Find AC/DC artist
        print("\n1. Testing artist search for 'AC/DC'...")
        cursor.execute("SELECT name, gid FROM artist WHERE name = 'AC/DC' LIMIT 1")
        result = cursor.fetchone()
        print(f"   Result: {result}")
        print(f"   Type: {type(result)}")
        
        if result:
            artist_mbid = result['gid']
            print(f"   Artist MBID: {artist_mbid}")
            
            # Test 2: Find recording by artist and title
            print("\n2. Testing recording search for 'Shot In The Dark'...")
            query = """
                SELECT r.name as title, r.gid as mbid
                FROM recording r
                JOIN artist_credit_name acn ON r.artist_credit = acn.artist_credit
                JOIN artist a ON acn.artist = a.id
                WHERE a.gid = %s AND r.name ILIKE %s
                LIMIT 1
            """
            cursor.execute(query, (artist_mbid, "%Shot In The Dark%"))
            recording_result = cursor.fetchone()
            print(f"   Result: {recording_result}")
            print(f"   Type: {type(recording_result)}")
            
            if recording_result:
                recording_mbid = recording_result['mbid']
                print(f"   Recording MBID: {recording_mbid}")
                
                # Test 3: Get recording info with artist credit
                print("\n3. Testing recording info with artist credit...")
                query = """
                    SELECT r.name as title, r.gid as mbid, ac.id as artist_credit_id
                    FROM recording r
                    JOIN artist_credit ac ON r.artist_credit = ac.id
                    WHERE r.gid = %s
                    LIMIT 1
                """
                cursor.execute(query, (recording_mbid,))
                info_result = cursor.fetchone()
                print(f"   Result: {info_result}")
                print(f"   Type: {type(info_result)}")
                
                if info_result:
                    # Test 4: Get artist credit details
                    print("\n4. Testing artist credit details...")
                    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, (info_result['artist_credit_id'],))
                    artists = cursor.fetchall()
                    print(f"   Artists: {artists}")
                    print(f"   Type: {type(artists)}")
                    
                    # Build artist string
                    artist_string = ""
                    for artist in artists:
                        artist_string += artist['name']
                        if artist['join_phrase']:
                            artist_string += artist['join_phrase']
                    
                    print(f"   Artist string: {artist_string}")
        
        cursor.close()
        connection.close()
        
    except Exception as e:
        print(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_database_queries() 