const fs = require('fs');

if (fs.existsSync('app_final.js')) {
    let js = fs.readFileSync('app_final.js', 'utf8');
    
    // Split into lines to find the actions declaration in renderTree
    const lines = js.split('\n');
    let inRenderTree = false;
    
    for (let i = 0; i < lines.length; i++) {
        if (lines[i].includes('function renderTree(parentId, level = 0, isVisible = true)')) {
            inRenderTree = true;
        }
        
        if (inRenderTree && lines[i].includes("let actions = '';")) {
            lines[i] = `      let actions = \`<button class="action-btn" style="background:#17a2b8; color:#fff; padding:5px 10px;" onclick="openAccountStatement(\${acc.id})">📄 كشف حساب</button>\`;`;
        }
        
        if (inRenderTree && lines[i].includes('actions = `<button class="action-btn btn-edit"')) {
            lines[i] = lines[i].replace('actions = ', 'actions += ');
            inRenderTree = false; // We found the target, stop looking
            break;
        }
    }
    
    fs.writeFileSync('app_final.js', lines.join('\n'), 'utf8');
    console.log('Added account statement button to app_final.js');
}

// 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');
}
