"""
List all users in the database
"""
import os
import sqlite3

def list_database_users():
    """List all users in the database"""
    
    # Determine the database path
    root_dir = os.path.dirname(os.path.abspath(__file__))
    instance_db = os.path.join(root_dir, 'instance', 'app.db')
    root_db = os.path.join(root_dir, 'app.db')
    
    if os.path.exists(instance_db):
        db_path = instance_db
    elif os.path.exists(root_db):
        db_path = root_db
    else:
        print(f"ERROR: Database not found at {instance_db} or {root_db}")
        return False
    
    print(f"Using database at: {db_path}")
    
    try:
        # Connect to the database
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        
        # Get list of all users
        cursor.execute("SELECT id, username, email, role FROM users")
        users = cursor.fetchall()
        
        print(f"Found {len(users)} users:")
        for user in users:
            user_id, username, email, role = user
            print(f"ID: {user_id} | Username: {username} | Role: {role} | Email: {email}")
            
        conn.close()
        return True
        
    except Exception as e:
        print(f"ERROR: {e}")
        return False

if __name__ == "__main__":
    list_database_users()