const fs = require('fs');

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

    // 1. Remove contact filters in forEach blocks
    // Pattern: if(c.type === 'عميل' || c.type === 'عميل/مورد') { ... }
    // We will just replace the strict if statement with a generic true or remove it.
    
    // Instead of complex AST parsing, we can replace the if conditions with if(true) or just comment them out.
    // Let's use regex to match if(c.type === '...' || ...)
    
    js = js.replace(/if\s*\(\s*c\.type\s*===[^)]+\)\s*\{/g, "if (true) { // removed type filter");

    // Also there are some .filter(c => c.type === ...) before forEach
    js = js.replace(/currentContacts\.filter\([^)]+c\.type\s*===[^)]+\)/g, "currentContacts");

    // Also fix any template literal that might not show the type name
    // contactSelect.innerHTML += `<option value="${c.id}">${c.name}</option>`;
    // change to include (${c.type})
    js = js.replace(/<option value="\$\{c\.id\}">\$\{c\.name\}<\/option>/g, '<option value="${c.id}">${c.name} (${c.type || \'\'})</option>');

    fs.writeFileSync(filename, js, '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!');
