const fs = require('fs');

// 1. UPDATE index.html
let html = fs.readFileSync('index.html', 'utf8');

const modalActionsRegex = /<button class="btn-secondary" onclick="closeModal\('client-abstract-modal'\)" style="font-size: 1\.1rem; padding: 10px 20px;">إلغاء<\/button>\s*<button class="btn-primary" id="btn-save-client-abstract" style="font-size: 1\.1rem; padding: 10px 30px;">حفظ المستخلص<\/button>/;

const newActionsHTML = `<button class="btn-secondary" onclick="closeModal('client-abstract-modal')" style="font-size: 1.1rem; padding: 10px 20px;">إلغاء</button>
        <button class="btn-primary" id="btn-save-client-abstract" style="font-size: 1.1rem; padding: 10px 30px;">حفظ المستخلص</button>
        <button class="btn-primary" id="btn-approve-client-abstract" style="font-size: 1.1rem; padding: 10px 30px; background:var(--success); color:#fff; border:none; display:none;" onclick="approveClientAbstractFromModal()">اعتماد المستخلص</button>
        <button class="btn-primary" id="btn-pay-client-abstract" style="font-size: 1.1rem; padding: 10px 30px; background:var(--success); color:#fff; border:none; display:none;" onclick="payClientAbstractFromModal()">سند قبض</button>
        <button class="btn-primary" id="btn-print-client-abstract" style="font-size: 1.1rem; padding: 10px 30px; background:#17a2b8; color:#fff; border:none; display:none;" onclick="printClientAbstractFromModal()">طباعة</button>`;

html = html.replace(modalActionsRegex, newActionsHTML);
fs.writeFileSync('index.html', html, 'utf8');

// 2. UPDATE app_final.js
let js = fs.readFileSync('app_final.js', 'utf8');

// Add the wrapper functions at the end of the file
const wrapperFunctions = `
window.approveClientAbstractFromModal = function() {
    const id = parseInt(document.getElementById('edit-client-abstract-id').value);
    if(id) {
        closeModal('client-abstract-modal');
        window.approveClientAbstract(id);
    }
};
window.payClientAbstractFromModal = function() {
    const id = parseInt(document.getElementById('edit-client-abstract-id').value);
    if(id) {
        closeModal('client-abstract-modal');
        window.payClientAbstract(id);
    }
};
window.printClientAbstractFromModal = function() {
    const id = parseInt(document.getElementById('edit-client-abstract-id').value);
    if(id) {
        window.printClientAbstract(id);
    }
};
`;

if (!js.includes('approveClientAbstractFromModal')) {
    js += wrapperFunctions;
}

// Update openClientAbstractModal to toggle buttons
const openModalRegex = /if\(ab\.status === 'approved' && btnSave\) btnSave\.style\.display = 'none';/;
const openModalReplacement = `if(ab.status === 'approved' && btnSave) {
          btnSave.style.display = 'none';
          document.getElementById('btn-approve-client-abstract').style.display = 'none';
          document.getElementById('btn-pay-client-abstract').style.display = 'inline-block';
          document.getElementById('btn-print-client-abstract').style.display = 'inline-block';
      } else {
          document.getElementById('btn-approve-client-abstract').style.display = 'inline-block';
          document.getElementById('btn-pay-client-abstract').style.display = 'none';
          document.getElementById('btn-print-client-abstract').style.display = 'inline-block';
      }`;
js = js.replace(openModalRegex, openModalReplacement);

// Also handle the if (!id) case where it's a new abstract
const newModalRegex = /btnSave\.style\.display = 'inline-block';/;
const newModalReplacement = `btnSave.style.display = 'inline-block';
      if(document.getElementById('btn-approve-client-abstract')) {
          document.getElementById('btn-approve-client-abstract').style.display = 'none';
          document.getElementById('btn-pay-client-abstract').style.display = 'none';
          document.getElementById('btn-print-client-abstract').style.display = 'none';
      }`;
js = js.replace(newModalRegex, newModalReplacement);

fs.writeFileSync('app_final.js', js, 'utf8');
console.log('Added modal actions!');
