const fs = require('fs');

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

const oldActionsRegex = /let actions = `[\s\S]*?`;/;

// Replace actions inside renderClientAbstracts specifically!
// I'll use a string replacement to be extremely precise.
const targetText = `let actions = \`
      <button class="action-btn btn-view" onclick="printClientAbstract(\${ab.id})">طباعة</button>
      \${ab.status === 'draft' ? \`<button class="action-btn" style="background:var(--success); color:#fff; border:none;" onclick="approveClientAbstract(\${ab.id})">اعتماد</button>\` : ''}
      \${ab.status === 'approved' ? \`<button class="action-btn" style="background:var(--success); color:#fff; border:none;" onclick="payClientAbstract(\${ab.id})">سند قبض</button>\` : ''}
      <button class="action-btn btn-edit" style="background:#17a2b8" onclick="openClientAbstractModal(\${ab.id})">\${ab.status === 'draft' ? 'تعديل' : 'عرض'}</button>
      \${ab.status === 'draft' ? \`<button class="action-btn btn-danger" onclick="deleteClientAbstract(\${ab.id})">حذف</button>\` : ''}
    \`;`;

const newActionsText = `let actions = \`
      <button class="action-btn" style="background:#6c757d; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem;" onclick="printClientAbstract(\${ab.id})">طباعة</button>
      \${ab.status === 'draft' ? \`<button class="action-btn" style="background:#28a745; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem;" onclick="approveClientAbstract(\${ab.id})">اعتماد</button>\` : ''}
      \${ab.status === 'approved' ? \`<button class="action-btn" style="background:#28a745; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem;" onclick="payClientAbstract(\${ab.id})">سند قبض</button>\` : ''}
      <button class="action-btn" style="background:#17a2b8; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem;" onclick="openClientAbstractModal(\${ab.id})">\${ab.status === 'draft' ? 'تعديل' : 'عرض'}</button>
      <button class="action-btn" style="background:#dc3545; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem;" onclick="deleteClientAbstract(\${ab.id})">حذف</button>
    \`;`;

let replaced = false;
let lines = js.split('\n');
for (let i = 0; i < lines.length; i++) {
    if (lines[i].includes('let actions = `') && lines[i+5].includes('openClientAbstractModal')) {
        lines[i] = `    let actions = \`
      <button class="action-btn" style="background:#6c757d; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem;" onclick="printClientAbstract(\${ab.id})">طباعة</button>
      \${ab.status === 'draft' ? \\\`<button class="action-btn" style="background:#28a745; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem;" onclick="approveClientAbstract(\${ab.id})">اعتماد</button>\\\` : ''}
      \${ab.status === 'approved' ? \\\`<button class="action-btn" style="background:#28a745; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem;" onclick="payClientAbstract(\${ab.id})">سند قبض</button>\\\` : ''}
      <button class="action-btn" style="background:#17a2b8; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem;" onclick="openClientAbstractModal(\${ab.id})">\${ab.status === 'draft' ? 'تعديل' : 'عرض'}</button>
      <button class="action-btn" style="background:#dc3545; color:#fff; border:none; padding: 4px 10px; border-radius: 4px; font-size: 0.9rem;" onclick="deleteClientAbstract(\${ab.id})">حذف</button>
    \`;`;
        lines[i+1] = '';
        lines[i+2] = '';
        lines[i+3] = '';
        lines[i+4] = '';
        lines[i+5] = '';
        lines[i+6] = '';
        replaced = true;
        break;
    }
}

if (replaced) {
    fs.writeFileSync('app_final.js', lines.join('\n'), 'utf8');
    console.log('Fixed buttons inside app_final.js!');
} else {
    console.log('Failed to find actions block!');
}

