const fs = require('fs');

function patchFile(filename) {
    if (!fs.existsSync(filename)) return;
    let js = fs.readFileSync(filename, 'utf8');

    const lines = js.split('\n');
    let modified = false;
    for(let i=0; i < lines.length; i++) {
        if(lines[i].includes('if(c.type ===') || lines[i].includes('if (c.type ===')) {
            lines[i] = '    if (true) { // removed type filter';
            modified = true;
        }
    }
    
    // Also remove the currentContacts.filter(c => ...) before forEach
    // actually they might be written as currentContacts.filter(...).forEach(...)
    for(let i=0; i < lines.length; i++) {
        if(lines[i].includes('currentContacts.filter') && lines[i].includes('.forEach(')) {
            lines[i] = lines[i].replace(/currentContacts\.filter\([^)]+\)\.forEach/, 'currentContacts.forEach');
            modified = true;
        }
    }

    if (modified) {
        fs.writeFileSync(filename, lines.join('\n'), 'utf8');
    }
}

patchFile('app_final.js');
patchFile('sales_purchases_module.js');
patchFile('vouchers_returns.js');
patchFile('subcontracts.js');
patchFile('fix_subcontracts.js');

// Also update HTML cache buster
let html = fs.readFileSync('index.html', 'utf8');
html = html.replace(/v=\d+/g, 'v=' + Date.now());
fs.writeFileSync('index.html', html, 'utf8');

console.log('Removed contact type filters second attempt!');
