const mysql = require('mysql2/promise');
require('dotenv').config();

const dbConfig = {
    host: process.env.DB_HOST || 'localhost',
    user: process.env.DB_USER || 'root',
    password: process.env.DB_PASS || '',
    database: process.env.DB_NAME || 'lovable_licenses'
};

async function test() {
    const conn = await mysql.createConnection(dbConfig);
    try {
        const isSuperAdmin = true; // Simulating RafaelTuber
        const userId = 1;

        let query = `
            SELECT id, username, role, credits, parent_id, is_active, created_at,
                   (SELECT COUNT(*) FROM licenses WHERE owner_id = users.id) as license_count
            FROM users
        `;
        let params = [];
        query += ' ORDER BY created_at DESC';

        const [users] = await conn.execute(query, params);
        console.log('Users found:', JSON.stringify(users, null, 2));
    } catch (e) {
        console.error('Error:', e.message);
    } finally {
        await conn.end();
    }
}

test();
