// Migration: Add group_id column to compliance_notes 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 add_compliance_notes_group_id migration...');

db.serialize(() => {
    db.run(`ALTER TABLE compliance_notes ADD COLUMN group_id TEXT`, (err) => {
        if (err) console.error('Error adding group_id column:', err);
        else console.log('✓ group_id column added to compliance_notes');
    });

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

    db.run(`UPDATE compliance_notes SET group_id = 'legacy-' || id WHERE group_id IS NULL`, (err) => {
        if (err) console.error('Error backfilling group_id:', err);
        else console.log('✓ Existing rows backfilled with legacy group_id');
    });
});

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