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);
    
    // Get provider connections
    try {
        const providers = db.exec("SELECT id, data FROM providerConnections");
        console.log('Provider Connections:');
        if (providers[0]?.values) {
            providers[0].values.forEach(row => {
                const data = JSON.parse(row[1]);
                console.log(`\n- ID: ${row[0]}`);
                console.log(`  Name: ${data.name}`);
                console.log(`  Type: ${data.type}`);
                console.log(`  Enabled: ${data.enabled}`);
                if (data.nodes) {
                    console.log(`  Nodes: ${data.nodes.length}`);
                }
            });
        }
    } catch (e) {
        console.log('Provider Connections error:', e.message);
    }
    
    // Get provider nodes
    try {
        const nodes = db.exec("SELECT id, data FROM providerNodes");
        console.log('\n\nProvider Nodes:');
        if (nodes[0]?.values) {
            nodes[0].values.forEach(row => {
                const data = JSON.parse(row[1]);
                console.log(`\n- ID: ${row[0]}`);
                console.log(`  Name: ${data.name}`);
                console.log(`  Base URL: ${data.baseUrl}`);
                console.log(`  Has API Key: ${data.apiKey ? 'yes' : 'no'}`);
                console.log(`  Enabled: ${data.enabled}`);
            });
        }
    } catch (e) {
        console.log('Provider Nodes error:', e.message);
    }
    
    db.close();
    process.exit(0);
});
