#!/usr/bin/env python
"""Simple test to confirm import system works"""
import pymysql
import sys

print("\n" + "="*80)
print("HAVEN GRAZURI - USER IMPORT SYSTEM TEST")
print("="*80)

try:
    # Test database connection
    print("\n[1/4] Testing database connection...")
    conn = pymysql.connect(
        host='localhost',
        user='root',
        password='password',
        database='xygbfpsg_graz',
        charset='utf8mb4'
    )
    print("    ✅ Database connection successful")
    
    cursor = conn.cursor()
    
    # Test user table exists
    print("\n[2/4] Checking Grazuri user table...")
    cursor.execute("SHOW TABLES LIKE 'user'")
    if cursor.fetchone():
        print("    ✅ Grazuri 'user' table exists")
        
        # Count users
        cursor.execute("SELECT COUNT(*) FROM user")
        count = cursor.fetchone()[0]
        print(f"    ✅ Found {count} users in table")
        
        if count > 0:
            # Fetch sample
            print("\n[3/4] Fetching sample users...")
            cursor.execute("SELECT userid, name, username, role FROM user LIMIT 5")
            users = cursor.fetchall()
            for user in users:
                print(f"    ✅ {user[1]} (@{user[2]}) - {user[3]}")
        else:
            print("    ⚠️  Table is empty - import populated SQL file first")
    else:
        print("    ⚠️  User table doesn't exist - import SQL file first")
    
    # Test Haven Grazuri users table
    print("\n[4/4] Checking Haven Grazuri users...")
    cursor.execute("SELECT COUNT(*) FROM users")
    haven_count = cursor.fetchone()[0]
    print(f"    ✅ {haven_count} users in Haven Grazuri system")
    
    conn.close()
    
    print("\n" + "="*80)
    print("✅ ALL TESTS PASSED - SYSTEM READY!")
    print("="*80)
    print("\n📝 To import users: python import_grazuri_users.py")
    print("\n")
    
except Exception as e:
    print(f"\n❌ ERROR: {e}")
    sys.exit(1)
