const fs = require('fs');
const readline = require('readline');

const filePath = '/Users/baris/Downloads/infosecl_platform (42).sql';

if (!fs.existsSync(filePath)) {
    console.error(`File not found: ${filePath}`);
    process.exit(1);
}

const fileStream = fs.createReadStream(filePath);
const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity });

console.log("Searching for IR and TI alerts...");
let countIR = 0;
let countTI = 0;

let processingAlerts = false;

rl.on('line', (line) => {
    line = line.trim();
    if (line.toUpperCase().startsWith('INSERT INTO `ALERTS`')) {
        processingAlerts = true;
    } else if (line.toUpperCase().startsWith('INSERT INTO `')) {
        processingAlerts = false;
    }
    if (!processingAlerts) return;
    if (!line.startsWith('(')) return;
    let content = line.substring(1).replace(/\);?$/, '').replace(/\),$/, '');
    
    // Simple split (assuming clean enough for path_code detection near end)
    // Use regex to find last few CSV values
    // path_code is 7th from end (index 15 out of 22)
    // Just regex search for 'TI' or 'IR' near the end of the string inside quotes?
    // Or full parse. Let's do full parse (reusing logic).
    
    const parts = [];
    let current = '';
    let inQuote = false;
    for (let i = 0; i < content.length; i++) {
        const char = content[i];
        if (char === '\\' && i + 1 < content.length) { i++; continue; }
        if (char === "'") inQuote = !inQuote;
        else if (char === ',' && !inQuote) { parts.push(current.trim()); current = ''; }
        else current += char;
    }
    parts.push(current.trim());
    
    if (parts.length >= 16) {
        let pathCode = parts[15].replace(/^'|'$/g, '');
        let id = parts[0];
        let title = parts[1].replace(/^'|'$/g, '');
        
        if (pathCode === 'TI' && countTI < 3) {
            console.log(`[TI] ID: ${id} - Title: ${title.substring(0,50)}...`);
            countTI++;
        }
        if (pathCode === 'IR' && countIR < 3) {
            console.log(`[IR] ID: ${id} - Title: ${title.substring(0,50)}...`);
            countIR++;
        }
    }
});
