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

import psycopg2
import sys

def test_connection():
    """Test database connection with various parameters."""
    
    # Test parameters
    test_configs = [
        {
            'host': 'db',
            'port': 5432,
            'database': 'musicbrainz_db',
            'user': 'musicbrainz',
            'password': 'musicbrainz',
            'description': 'Docker service name'
        },
        {
            'host': '172.18.0.2',
            'port': 5432,
            'database': 'musicbrainz_db',
            'user': 'musicbrainz',
            'password': 'musicbrainz',
            'description': 'Docker container IP'
        },
        {
            'host': 'localhost',
            'port': 5432,
            'database': 'musicbrainz_db',
            'user': 'musicbrainz',
            'password': 'musicbrainz',
            'description': 'Localhost'
        }
    ]
    
    for config in test_configs:
        print(f"\nTesting connection to {config['host']} ({config['description']})...")
        try:
            conn = psycopg2.connect(
                host=config['host'],
                port=config['port'],
                database=config['database'],
                user=config['user'],
                password=config['password'],
                connect_timeout=5
            )
            
            print(f"✅ Successfully connected to {config['host']}")
            
            # 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 a specific artist
            cursor.execute("SELECT name FROM artist WHERE name = 'The Beatles' LIMIT 1")
            result = cursor.fetchone()
            if result:
                print(f"🎵 Found artist: {result[0]}")
            else:
                print("❌ The Beatles not found")
            
            conn.close()
            return True
            
        except Exception as e:
            print(f"❌ Failed to connect to {config['host']}: {e}")
            continue
    
    return False

if __name__ == "__main__":
    print("🧪 Simple Database Connection Test")
    print("=" * 40)
    
    success = test_connection()
    
    if success:
        print("\n✅ Database connection test successful!")
    else:
        print("\n❌ All connection attempts failed")
        sys.exit(1) 