#!/usr/bin/env python3
"""
Test script to verify database connection from inside Docker container.
"""

import psycopg2
from fuzzywuzzy import fuzz
import sys

def test_db_connection():
    """Test database connection from inside Docker."""
    try:
        print("Testing database connection from inside Docker...")
        
        # Connect using Docker network hostname
        conn = psycopg2.connect(
            host="db",  # Docker service name
            port=5432,
            database="musicbrainz_db",
            user="musicbrainz",
            password="musicbrainz",
            connect_timeout=10
        )
        print("✅ Successfully connected to database")
        
        # Test a simple query
        cursor = conn.cursor()
        cursor.execute("SELECT COUNT(*) FROM artist")
        count = cursor.fetchone()[0]
        print(f"📊 Found {count:,} artists in database")
        
        # Test fuzzy search
        test_fuzzy_search(cursor)
        
        conn.close()
        return True
        
    except Exception as e:
        print(f"❌ Database connection failed: {e}")
        return False

def test_fuzzy_search(cursor):
    """Test fuzzy search functionality."""
    print("\n🔍 Testing fuzzy search...")
    
    test_artists = [
        "The Beatles",
        "Queen", 
        "Pink Floyd",
        "Coldplay",
        "AC/DC",
        "Bruno Mars",
        "Taylor Swift"
    ]
    
    for artist in test_artists:
        print(f"\nSearching for: {artist}")
        
        # First try exact match
        cursor.execute("SELECT name FROM artist WHERE name = %s", (artist,))
        exact_matches = cursor.fetchall()
        
        if exact_matches:
            print(f"  ✅ Exact match found: {exact_matches[0][0]}")
            continue
        
        # If no exact match, try fuzzy search
        cursor.execute("SELECT name FROM artist LIMIT 1000")
        artists = [row[0] for row in cursor.fetchall()]
        
        # Find best match
        best_match = None
        best_score = 0
        
        for db_artist in artists:
            score = fuzz.ratio(artist.lower(), db_artist.lower())
            if score > best_score:
                best_score = score
                best_match = db_artist
        
        if best_score >= 80:
            print(f"  ✅ Found: {best_match} (score: {best_score})")
        else:
            print(f"  ❌ No good match found (best: {best_match} with score {best_score})")

if __name__ == "__main__":
    print("🧪 Testing MusicBrainz Database Connection from Docker")
    print("=" * 60)
    
    success = test_db_connection()
    
    if success:
        print("\n✅ Database connection and fuzzy search test completed!")
    else:
        print("\n❌ Database connection test failed")
        sys.exit(1) 