import odoo
import json

def check_demo_users(cr):
    """Verificar si los usuarios demo fueron creados"""
    cr.execute("""
        SELECT 
            u.id,
            u.login,
            u.name,
            u.active,
            array_agg(g.name) as groups
        FROM res_users u
        LEFT JOIN res_groups_users_rel rel ON rel.uid = u.id
        LEFT JOIN res_groups g ON g.id = rel.gid
        WHERE u.login IN ('recepcionista', 'tecnico', 'administrador')
        GROUP BY u.id, u.login, u.name, u.active
        ORDER BY u.login
    """)
    
    users = cr.fetchall()
    
    print("\n=== USUARIOS DEMO CREADOS ===")
    print("-" * 60)
    
    if not users:
        print("❌ NO se encontraron usuarios demo")
        return
    
    for user in users:
        user_id, login, name, active, groups = user
        status = "✓ Activo" if active else "✗ Inactivo"
        print(f"\nUsuario: {login}")
        print(f"  ID: {user_id}")
        print(f"  Nombre: {name}")
        print(f"  Estado: {status}")
        print(f"  Grupos: {', '.join(groups) if groups[0] else 'Sin grupos'}")
    
    print("\n" + "-" * 60)
    print(f"Total usuarios demo encontrados: {len(users)}")
    
    # Verificar contraseñas (solo para confirmar que pueden loguearse)
    expected_users = {
        'recepcionista': 'Recepcionista Demo',
        'tecnico': 'Técnico Demo', 
        'administrador': 'Administrador Lab Demo'
    }
    
    missing = []
    for login, expected_name in expected_users.items():
        if not any(u[1] == login for u in users):
            missing.append(login)
    
    if missing:
        print(f"\n⚠️  Usuarios faltantes: {', '.join(missing)}")
    else:
        print("\n✅ Todos los usuarios demo esperados fueron creados")

if __name__ == '__main__':
    db_name = 'lims_demo'
    registry = odoo.registry(db_name)
    with registry.cursor() as cr:
        check_demo_users(cr)