const fs = require('fs');

if (fs.existsSync('app_final.js')) {
    let appJs = fs.readFileSync('app_final.js', 'utf8');
    appJs = appJs.replace(
        "if (typeof renderPurchases === 'function') renderPurchases();",
        "if (typeof renderPurchases === 'function') renderPurchases();\n    if (typeof renderVouchers === 'function') renderVouchers();\n    if (typeof renderSalesReturns === 'function') renderSalesReturns();\n    if (typeof renderPurchasesReturns === 'function') renderPurchasesReturns();"
    );
    fs.writeFileSync('app_final.js', appJs, 'utf8');
    console.log('Patched app_final.js with loadData refreshes');
}

if (fs.existsSync('vouchers_returns.js')) {
    let vrJs = fs.readFileSync('vouchers_returns.js', 'utf8');
    
    // Fix Voucher Actions
    const oldVoucherBtn = `<button class="btn-primary" onclick="editVoucher(\${v.id})" style="padding:5px;">تعديل/عرض</button>
        \${currentUser && currentUser.role === 'مدير عام' ? \`<button class="btn-danger" onclick="deleteInvoice(\${v.id}, 'voucher')" style="padding:5px;">حذف</button>\` : ''}`;
        
    const newVoucherBtn = `<div style="display:flex; gap:5px; justify-content:center;">
          <button class="action-btn btn-edit" onclick="editVoucher(\${v.id})" style="padding:5px 10px;">تعديل/عرض</button>
          \${currentUser && currentUser.role === 'مدير عام' ? \`<button class="action-btn btn-delete" onclick="deleteInvoice(\${v.id}, 'voucher')" style="padding:5px 10px;">حذف</button>\` : ''}
        </div>`;
    
    // Fix Sales Returns Actions
    const oldSRBtn = `<button class="btn-primary" onclick="editSalesReturn(\${r.id})" style="padding:5px;">تعديل/عرض</button>
        \${currentUser && currentUser.role === 'مدير عام' ? \`<button class="btn-danger" onclick="deleteInvoice(\${r.id}, 'sales_return')" style="padding:5px;">حذف</button>\` : ''}`;
        
    const newSRBtn = `<div style="display:flex; gap:5px; justify-content:center;">
          <button class="action-btn btn-edit" onclick="editSalesReturn(\${r.id})" style="padding:5px 10px;">تعديل/عرض</button>
          \${currentUser && currentUser.role === 'مدير عام' ? \`<button class="action-btn btn-delete" onclick="deleteInvoice(\${r.id}, 'sales_return')" style="padding:5px 10px;">حذف</button>\` : ''}
        </div>`;
        
    // Fix Purchases Returns Actions
    const oldPRBtn = `<button class="btn-primary" onclick="editPurchasesReturn(\${r.id})" style="padding:5px;">تعديل/عرض</button>
        \${currentUser && currentUser.role === 'مدير عام' ? \`<button class="btn-danger" onclick="deleteInvoice(\${r.id}, 'purchases_return')" style="padding:5px;">حذف</button>\` : ''}`;
        
    const newPRBtn = `<div style="display:flex; gap:5px; justify-content:center;">
          <button class="action-btn btn-edit" onclick="editPurchasesReturn(\${r.id})" style="padding:5px 10px;">تعديل/عرض</button>
          \${currentUser && currentUser.role === 'مدير عام' ? \`<button class="action-btn btn-delete" onclick="deleteInvoice(\${r.id}, 'purchases_return')" style="padding:5px 10px;">حذف</button>\` : ''}
        </div>`;

    // Regex replacement for broken encodings just in case
    // For vouchers
    vrJs = vrJs.replace(/<button class="btn-primary" onclick="editVoucher\([^>]+>.*?<\/button>\s*\$\{[^?]+\?\s*`<button class="btn-danger"[^>]+>.*?<\/button>`\s*:\s*''\}/g, newVoucherBtn);
    
    // For sales returns
    vrJs = vrJs.replace(/<button class="btn-primary" onclick="editSalesReturn\([^>]+>.*?<\/button>\s*\$\{[^?]+\?\s*`<button class="btn-danger"[^>]+>.*?<\/button>`\s*:\s*''\}/g, newSRBtn);
    
    // For purchases returns
    vrJs = vrJs.replace(/<button class="btn-primary" onclick="editPurchasesReturn\([^>]+>.*?<\/button>\s*\$\{[^?]+\?\s*`<button class="btn-danger"[^>]+>.*?<\/button>`\s*:\s*''\}/g, newPRBtn);

    fs.writeFileSync('vouchers_returns.js', vrJs, 'utf8');
    console.log('Patched vouchers_returns.js buttons');
}

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