#!/usr/bin/env python3
"""
Test script for musicbrainz-cleaner database connection.
"""

import sys
import os

# Add the src directory to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))

from api.database import MusicBrainzDatabase

def test_cleaner_database():
    """Test the musicbrainz-cleaner database connection."""
    print("🧪 Testing MusicBrainz Cleaner Database Connection")
    print("=" * 50)
    
    try:
        # Create database instance
        db = MusicBrainzDatabase()
        
        # Test connection
        if not db.connect():
            print("❌ Failed to connect to database")
            return False
        
        # Test a simple query
        cursor = db.cursor
        cursor.execute("SELECT COUNT(*) FROM artist")
        count = cursor.fetchone()['count']
        print(f"📊 Found {count:,} artists in database")
        
        # Test fuzzy search
        print("\n🔍 Testing fuzzy search...")
        result = db.fuzzy_search_artist("The Beatles")
        if result:
            artist_name, mbid, score = result
            print(f"✅ Found artist: {artist_name} (MBID: {mbid}, Score: {score:.2f})")
        else:
            print("❌ No artist found")
        
        # Test recording search
        print("\n🎵 Testing recording search...")
        if result:
            recording_result = db.fuzzy_search_recording("Hey Jude", result[1])
            if recording_result:
                recording_title, recording_mbid, recording_score = recording_result
                print(f"✅ Found recording: {recording_title} (MBID: {recording_mbid}, Score: {recording_score:.2f})")
            else:
                print("❌ No recording found")
        
        # Disconnect
        db.disconnect()
        
        print("\n✅ MusicBrainz Cleaner database test successful!")
        return True
        
    except Exception as e:
        print(f"❌ Test failed: {e}")
        return False

if __name__ == "__main__":
    success = test_cleaner_database()
    if not success:
        sys.exit(1) 