const fs = require('fs');
let html = fs.readFileSync('index.html', 'utf8');

const missingBlock = `

  <!-- Voucher Modal -->
  <div id="voucher-modal" class="modal" style="z-index: 9999999 !important;">
    <div class="modal-content" style="width: 500px; max-width: 95%; max-height: 90vh; overflow-y: auto;">
      <h3 id="voucher-modal-title">إنشاء سند</h3>
      <input type="hidden" id="edit-voucher-id">
      <input type="hidden" id="voucher-type">
      <div class="flex-col" style="margin-bottom: 10px;">
        <label>التاريخ</label>
        <input type="datetime-local" id="voucher-date">
      </div>
      <div class="flex-col" style="margin-bottom: 10px;">
        <label>رقم المستند / المرجع</label>
        <input type="text" id="voucher-doc-no" placeholder="اختياري">`;

html = html.replace(/<\/div>\s*<\/div>\s*<\/div>\s*<\/div>\s*<div class="flex-col" style="margin-bottom: 10px;">\s*<label id="voucher-contact-label">جهة \/ مستفيد<\/label>/, 
`</div>
    </div>
  </div>${missingBlock}
      </div>
      <div class="flex-col" style="margin-bottom: 10px;">
        <label id="voucher-contact-label">جهة / مستفيد</label>`);

// Also apply the cache buster safely at the end of the file
html = html.replace(/<script src="db\.js(?!.*?\?)"><\/script>/, '<script src="db.js?v=202606030939"></script>');
html = html.replace(/<script src="app_final\.js\?v=\d+" defer><\/script>/, '<script src="app_final.js?v=202606030939" defer></script>');
html = html.replace(/<script src="vouchers_returns\.js\?v=\d+" defer><\/script>/, '<script src="vouchers_returns.js?v=202606030939" defer></script>');

fs.writeFileSync('index.html', html, 'utf8');
console.log('Restored voucher modal and updated cache busters safely.');
