// Migration: Add atlas_action_plans_cache 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 Atlas action plans cache migration...');

db.serialize(() => {
    // Cache table — one row per host, holding cached Atlas action plan status
    db.run(`
        CREATE TABLE IF NOT EXISTS atlas_action_plans_cache (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            host_id INTEGER NOT NULL UNIQUE,
            has_action_plan INTEGER NOT NULL DEFAULT 0,
            plan_count INTEGER NOT NULL DEFAULT 0,
            plans_json TEXT NOT NULL DEFAULT '[]',
            synced_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
        )
    `, (err) => {
        if (err) console.error('Error creating atlas_action_plans_cache table:', err);
        else console.log('✓ atlas_action_plans_cache table created');
    });

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

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