// Migration: Add ivanti_todo_queue 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_todo_queue migration...');

db.serialize(() => {
    db.run(`
        CREATE TABLE IF NOT EXISTS ivanti_todo_queue (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            user_id INTEGER NOT NULL,
            finding_id TEXT NOT NULL,
            finding_title TEXT,
            cves_json TEXT,
            vendor TEXT NOT NULL,
            workflow_type TEXT NOT NULL CHECK(workflow_type IN ('FP', 'Archer')),
            status TEXT NOT NULL DEFAULT 'pending' CHECK(status IN ('pending', 'complete')),
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
        )
    `, (err) => {
        if (err) console.error('Error creating table:', err);
        else console.log('✓ ivanti_todo_queue table created');
    });

    db.run(
        'CREATE INDEX IF NOT EXISTS idx_todo_queue_user ON ivanti_todo_queue(user_id, status)',
        (err) => {
            if (err) console.error('Error creating index:', err);
            else console.log('✓ User+status index created');
        }
    );

    console.log('✓ Migration statements queued');
});

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