// Migration: Add ivanti_findings_cache and ivanti_finding_notes tables
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 findings tables migration...');

db.serialize(() => {
    // Cache table — single row holding the latest sync result
    db.run(`
        CREATE TABLE IF NOT EXISTS ivanti_findings_cache (
            id INTEGER PRIMARY KEY CHECK (id = 1),
            total INTEGER DEFAULT 0,
            findings_json TEXT DEFAULT '[]',
            synced_at DATETIME,
            sync_status TEXT DEFAULT 'never',
            error_message TEXT
        )
    `, (err) => {
        if (err) console.error('Error creating findings cache table:', err);
        else console.log('✓ ivanti_findings_cache table created');
    });

    db.run(`
        INSERT OR IGNORE INTO ivanti_findings_cache (id, total, findings_json, sync_status)
        VALUES (1, 0, '[]', 'never')
    `, (err) => {
        if (err) console.error('Error seeding findings cache row:', err);
        else console.log('✓ ivanti_findings_cache row seeded');
    });

    // Notes table — one row per finding, persists across cache refreshes
    db.run(`
        CREATE TABLE IF NOT EXISTS ivanti_finding_notes (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            finding_id TEXT NOT NULL UNIQUE,
            note TEXT NOT NULL DEFAULT '',
            updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
        )
    `, (err) => {
        if (err) console.error('Error creating finding notes table:', err);
        else console.log('✓ ivanti_finding_notes table created');
    });

    db.run(`
        CREATE INDEX IF NOT EXISTS idx_finding_notes_finding_id
        ON ivanti_finding_notes(finding_id)
    `, (err) => {
        if (err) console.error('Error creating notes index:', err);
        else console.log('✓ finding_id index created');
    });
});

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