// Migration: Add ivanti_sync_state table
const sqlite3 = require('sqlite3').verbose();
const path = require('path');

const dbPath = path.join(__dirname, '..', 'cve_database.db');
const db = new sqlite3.Database(dbPath);

console.log('Starting Ivanti sync state migration...');

db.serialize(() => {
    db.run(`
        CREATE TABLE IF NOT EXISTS ivanti_sync_state (
            id INTEGER PRIMARY KEY CHECK (id = 1),
            total INTEGER DEFAULT 0,
            workflows_json TEXT DEFAULT '[]',
            synced_at DATETIME,
            sync_status TEXT DEFAULT 'never',
            error_message TEXT
        )
    `, (err) => {
        if (err) console.error('Error creating table:', err);
        else console.log('✓ ivanti_sync_state table created');
    });

    // Seed the single-row state record
    db.run(`
        INSERT OR IGNORE INTO ivanti_sync_state (id, total, workflows_json, sync_status)
        VALUES (1, 0, '[]', 'never')
    `, (err) => {
        if (err) console.error('Error seeding state row:', err);
        else console.log('✓ ivanti_sync_state row seeded');
    });
});

db.close(() => {
    console.log('Migration complete!');
});
