const fs = require('fs');

if (fs.existsSync('app_final.js')) {
    let js = fs.readFileSync('app_final.js', 'utf8');
    
    // Add account statement button unconditionally
    const regex = /let actions = '';\s*if\(currentUser && currentUser\.role === 'مدير عام'\) \{\s*actions = `<button class="action-btn btn-edit"/;
    
    const replaceString = `let actions = \`<button class="action-btn" style="background:#17a2b8; color:#fff; padding:5px 10px;" onclick="openAccountStatement(\${acc.id})">📄 كشف حساب</button>\`;
      if(currentUser && currentUser.role === 'مدير عام') {
        actions += \` <button class="action-btn btn-edit"`;
        
    if (regex.test(js) && !js.includes('📄 كشف حساب</button>')) {
        js = js.replace(regex, replaceString);
        fs.writeFileSync('app_final.js', js, 'utf8');
        console.log('Added account statement button to app_final.js using regex');
    } else {
        console.log('Regex did not match or already replaced.');
    }
}

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