#!/usr/bin/env python3
"""Test MariaDB connectivity from Windows"""
import pymysql

# Connection details
HOST = '172.16.3.20'
PORT = 3306
ROOT_PASSWORD = r'Dy8RPj-s{+=bP^(NoW"T;E~JXyBC9u|<'
CLAUDETOOLS_PASSWORD = 'CT_e8fcd5a3952030a79ed6debae6c954ed'

print("Testing MariaDB connection to Jupiter (172.16.3.20)...\n")

# Test 1: Root connection
try:
    print("Test 1: Connecting as root...")
    conn = pymysql.connect(
        host=HOST,
        port=PORT,
        user='root',
        password=ROOT_PASSWORD,
        connect_timeout=10
    )
    print("[OK] Root connection successful!")

    cursor = conn.cursor()
    cursor.execute("SELECT VERSION()")
    version = cursor.fetchone()
    print(f"   MariaDB Version: {version[0]}")

    cursor.execute("SHOW DATABASES")
    databases = cursor.fetchall()
    print(f"   Databases found: {len(databases)}")
    for db in databases:
        print(f"      - {db[0]}")

    # Check if claudetools database exists
    cursor.execute("SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = 'claudetools'")
    claudetools_db = cursor.fetchone()
    if claudetools_db:
        print("\n[OK] 'claudetools' database exists!")
    else:
        print("\n[WARNING] 'claudetools' database does NOT exist yet")

    # Check if claudetools user exists
    cursor.execute("SELECT User FROM mysql.user WHERE User = 'claudetools'")
    claudetools_user = cursor.fetchone()
    if claudetools_user:
        print("[OK] 'claudetools' user exists!")
    else:
        print("[WARNING] 'claudetools' user does NOT exist yet")

    conn.close()
    print("\nTest 1 PASSED [OK]\n")

except Exception as e:
    print(f"[FAILED] Test 1: {e}\n")

# Test 2: Claudetools user connection (if exists)
try:
    print("Test 2: Connecting as 'claudetools' user...")
    conn = pymysql.connect(
        host=HOST,
        port=PORT,
        user='claudetools',
        password=CLAUDETOOLS_PASSWORD,
        database='claudetools',
        connect_timeout=10
    )
    print("[OK] Claudetools user connection successful!")

    cursor = conn.cursor()
    cursor.execute("SELECT DATABASE()")
    current_db = cursor.fetchone()
    print(f"   Current database: {current_db[0]}")

    cursor.execute("SHOW TABLES")
    tables = cursor.fetchall()
    print(f"   Tables in claudetools: {len(tables)}")

    conn.close()
    print("\nTest 2 PASSED [OK]\n")

except pymysql.err.OperationalError as e:
    if "Access denied" in str(e):
        print("[WARNING] Claudetools user doesn't exist or wrong password")
    elif "Unknown database" in str(e):
        print("[WARNING] Claudetools database doesn't exist yet")
    else:
        print(f"[WARNING] Test 2: {e}")
    print("   (This is expected if database/user haven't been created yet)\n")
except Exception as e:
    print(f"[WARNING] Test 2: {e}\n")

print("\n" + "="*60)
print("CONNECTION TEST SUMMARY")
print("="*60)
print("[OK] MariaDB is accessible from Windows on port 3306")
print("[OK] Root authentication works")
print("\nNext step: Create 'claudetools' database and user if they don't exist")
