const { execFile } = require('child_process');
const fs = require('fs');
const path = require('path');
const os = require('os');

function getCursorStateDbPath() {
    if (process.platform === 'darwin') {
        return path.join(os.homedir(), 'Library/Application Support/Cursor/User/globalStorage/state.vscdb');
    }
    return '';
}

async function dumpBubbles() {
    const dbPath = getCursorStateDbPath();
    if (!dbPath || !fs.existsSync(dbPath)) {
        console.error('DB not found at:', dbPath);
        return;
    }

    const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'debug-cursor-'));
    const snapshotPath = path.join(tempDir, 'state.vscdb');

    console.log('Creating snapshot of:', dbPath);
    try {
        // Simple copy for debug
        fs.copyFileSync(dbPath, snapshotPath);

        const sql = "SELECT rowid, key, value FROM cursorDiskKV WHERE key GLOB 'bubbleId:*' ORDER BY rowid DESC LIMIT 10";
        execFile('sqlite3', ['-json', snapshotPath, sql], (err, stdout, stderr) => {
            if (err) {
                console.error('Error running sqlite3:', stderr || err.message);
                return;
            }
            console.log('Recent 10 bubbles:');
            console.log(stdout);

            // Clean up
            fs.rmSync(tempDir, { recursive: true, force: true });
        });
    } catch (e) {
        console.error('Snapshot error:', e.message);
    }
}

dumpBubbles();
