#!/usr/bin/env python3
"""
Simple database connection test for MusicBrainz.
"""

import psycopg2
import sys

def test_simple_connection():
    """Test basic database connection."""
    try:
        # Try to connect to the database
        print("🔌 Testing database connection...")
        
        # Try different connection methods
        hosts = ["127.0.0.1", "localhost", "172.18.0.2", "db"]
        
        for host in hosts:
            print(f"  Trying {host}...")
            try:
                conn = psycopg2.connect(
                    host=host,
                    port=5432,
                    database="musicbrainz",
                    user="musicbrainz",
                    password="musicbrainz",
                    connect_timeout=5
                )
                print(f"  ✅ Connected to {host}")
                
                # Test basic query
                cursor = conn.cursor()
                cursor.execute("SELECT version();")
                version = cursor.fetchone()[0]
                print(f"  📊 PostgreSQL version: {version.split(',')[0]}")
                
                # Check if any tables exist
                cursor.execute("""
                    SELECT table_name 
                    FROM information_schema.tables 
                    WHERE table_schema = 'public'
                    LIMIT 5;
                """)
                tables = cursor.fetchall()
                
                if tables:
                    print(f"  📋 Found {len(tables)} tables:")
                    for table in tables:
                        print(f"    - {table[0]}")
                else:
                    print("  ⚠️  No tables found in database")
                
                conn.close()
                return True
                
            except Exception as e:
                print(f"  ❌ Failed to connect to {host}: {e}")
                continue
                
    except Exception as e:
        print(f"❌ Database connection failed: {e}")
        return False

if __name__ == "__main__":
    print("🧪 Simple MusicBrainz Database Connection Test")
    print("=" * 50)
    
    success = test_simple_connection()
    
    if success:
        print("\n✅ Database connection test completed!")
        print("💡 The database is accessible but may be empty")
        print("💡 Wait for MusicBrainz data import to complete")
    else:
        print("\n❌ Database connection test failed")
        sys.exit(1) 