const initSqlJs = require('sql.js');
const fs = require('fs');

initSqlJs().then(SQL => {
    const buffer = fs.readFileSync('E:/var/lib/9router/db/data.sqlite');
    const db = new SQL.Database(buffer);
    
    // List all tables
    const tables = db.exec("SELECT name FROM sqlite_master WHERE type='table'");
    console.log('Tables:', tables[0]?.values.map(v => v[0]).join(', '));
    
    // Get providers
    try {
        const providers = db.exec("SELECT id, data FROM providers");
        console.log('\nProviders:', providers[0]?.columns);
        if (providers[0]?.values) {
            providers[0].values.forEach(row => {
                const data = JSON.parse(row[1]);
                console.log(`- ${row[0]}: ${data.name || 'unknown'}, enabled: ${data.enabled}`);
            });
        }
    } catch (e) {
        console.log('Providers error:', e.message);
    }
    
    // Get API keys
    try {
        const keys = db.exec("SELECT id, key, name FROM apiKeys");
        console.log('\nAPI Keys:', keys[0]?.columns);
        if (keys[0]?.values) {
            keys[0].values.forEach(row => {
                console.log(`- ${row[2]}: ${row[1]?.substring(0, 10)}...`);
            });
        }
    } catch (e) {
        console.log('API Keys error:', e.message);
    }
    
    db.close();
    process.exit(0);
});
