const fs = require('fs');

try {
    let indexHtml = fs.readFileSync('index.html', 'utf8');

    // Add 'sales-paid-total' to index.html
    if (!indexHtml.includes('sales-paid-total')) {
        indexHtml = indexHtml.replace(
            /<div style="color: var\(--danger\);">المتبقي: <span id="sales-remaining">0<\/span><\/div>/g,
            `<div style="color: #28a745;">المسدد السندات: <span id="sales-paid-total">0</span></div>\n        <div style="color: var(--danger);">المتبقي: <span id="sales-remaining">0</span></div>`
        );
    }
    
    // Add 'purchases-paid-total' to index.html
    if (!indexHtml.includes('purchases-paid-total')) {
        indexHtml = indexHtml.replace(
            /<div style="color: var\(--danger\);">المتبقي: <span id="purchases-remaining">0<\/span><\/div>/g,
            `<div style="color: #28a745;">المسدد السندات: <span id="purchases-paid-total">0</span></div>\n        <div style="color: var(--danger);">المتبقي: <span id="purchases-remaining">0</span></div>`
        );
    }

    const timestamp = Date.now();
    indexHtml = indexHtml.replace(/app_final\.js\?v=\d+/g, 'app_final.js?v=' + timestamp);
    fs.writeFileSync('index.html', indexHtml, 'utf8');

    let appFinalJs = fs.readFileSync('app_final.js', 'utf8');

    // Update renderSalesLines
    appFinalJs = appFinalJs.replace(
        /const net = subtotal - totalDiscountValue;\n  document\.getElementById\('sales-net-total'\)\.innerText = net\.toLocaleString\('ar-EG', \{minimumFractionDigits:2\}\);/g,
        `const net = subtotal - totalDiscountValue;\n  document.getElementById('sales-net-total').innerText = net.toLocaleString('ar-EG', {minimumFractionDigits:2});\n  const idStr = document.getElementById('edit-sales-id').value;\n  let paid = 0;\n  if(idStr) {\n    const id = parseInt(idStr);\n    paid = allInvoices.filter(t => (t.type === 'receipt' || t.type === 'payment') && t.status === 'posted' && String(t.linked_invoice_id) === String(id)).reduce((sum, v) => sum + (parseFloat(v.total_amount) || 0), 0);\n  }\n  document.getElementById('sales-paid-total').innerText = paid.toLocaleString('ar-EG', {minimumFractionDigits:2});\n  document.getElementById('sales-remaining').innerText = (net - paid).toLocaleString('ar-EG', {minimumFractionDigits:2});`
    );

    // Update renderPurchasesLines
    appFinalJs = appFinalJs.replace(
        /const net = subtotal - totalDiscountValue;\n  document\.getElementById\('purchases-net-total'\)\.innerText = net\.toLocaleString\('ar-EG', \{minimumFractionDigits:2\}\);/g,
        `const net = subtotal - totalDiscountValue;\n  document.getElementById('purchases-net-total').innerText = net.toLocaleString('ar-EG', {minimumFractionDigits:2});\n  const idStr = document.getElementById('edit-purchases-id').value;\n  let paid = 0;\n  if(idStr) {\n    const id = parseInt(idStr);\n    paid = allInvoices.filter(t => (t.type === 'receipt' || t.type === 'payment') && t.status === 'posted' && String(t.linked_invoice_id) === String(id)).reduce((sum, v) => sum + (parseFloat(v.total_amount) || 0), 0);\n  }\n  document.getElementById('purchases-paid-total').innerText = paid.toLocaleString('ar-EG', {minimumFractionDigits:2});\n  document.getElementById('purchases-remaining').innerText = (net - paid).toLocaleString('ar-EG', {minimumFractionDigits:2});`
    );

    fs.writeFileSync('app_final.js', appFinalJs, 'utf8');
    console.log('Added paid amount display to modals successfully.');
} catch (e) {
    console.error('Error patching:', e);
}
