#!/usr/bin/env python3
"""
Test the ILIKE query that's causing the Eazy-E to match Sleazy-E.
"""

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_ilike_eazy_e():
    """Test the ILIKE query for Eazy-E."""
    print("🔍 Testing ILIKE query for Eazy-E...")
    
    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 the exact query from fuzzy_search_artist
        main_artist = "Eazy-E"
        query = "SELECT name, gid FROM artist WHERE name ILIKE %s LIMIT 50"
        pattern = f"%{main_artist}%"
        
        print(f"\n1. Testing query: {query}")
        print(f"   Pattern: {pattern}")
        
        cursor.execute(query, (pattern,))
        artists = cursor.fetchall()
        
        print(f"\n2. Found {len(artists)} matches:")
        for artist in artists:
            print(f"     {artist['name']} (MBID: {artist['gid']})")
        
        # Test fuzzy ratio on these results
        print(f"\n3. Fuzzy ratio scores for 'Eazy-E':")
        from fuzzywuzzy import fuzz
        
        best_match = None
        best_score = 0
        
        for artist in artists:
            score = fuzz.ratio(main_artist.lower(), artist['name'].lower())
            print(f"     {artist['name']} - Score: {score}")
            if score > best_score:
                best_score = score
                best_match = artist
        
        print(f"\n4. Best match: {best_match['name']} (Score: {best_score})")
        
        cursor.close()
        connection.close()
        
    except Exception as e:
        print(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_ilike_eazy_e() 