const fs = require('fs');

if (fs.existsSync('update_pay_client_abstract.js')) {
    let js = fs.readFileSync('update_pay_client_abstract.js', 'utf8');
    js = js.replace(
        /suggestedAmount = Math\.max\(0, parseFloat\(ab\.net_value\) - totalPaid\);/g,
        "let remaining = parseFloat(ab.net_value) - totalPaid;\n        remaining = Math.round(remaining * 100) / 100;\n        suggestedAmount = Math.max(0, remaining);"
    );
    fs.writeFileSync('update_pay_client_abstract.js', js, 'utf8');
}

if (fs.existsSync('app_final.js')) {
    let js = fs.readFileSync('app_final.js', 'utf8');
    js = js.replace(
        /suggestedAmount = Math\.max\(0, parseFloat\(ab\.net_value\) - totalPaid\);/g,
        "let remaining = parseFloat(ab.net_value) - totalPaid;\n        remaining = Math.round(remaining * 100) / 100;\n        suggestedAmount = Math.max(0, remaining);"
    );
    fs.writeFileSync('app_final.js', js, 'utf8');
}

console.log('Client abstract payment patched with rounding!');
