const fs = require('fs');

if (fs.existsSync('index.html')) {
    let html = fs.readFileSync('index.html', 'utf8');
    
    // Fix invisible text in totals table (add color: #333)
    html = html.replace('<td style="padding: 15px; background: #f8f9fa; width: 50%; border: none; border-bottom: 1px solid #dee2e6;">إجمالي المدين</td>', '<td style="padding: 15px; background: #f8f9fa; color: #333; font-weight: bold; width: 50%; border: none; border-bottom: 1px solid #dee2e6;">إجمالي المدين</td>');
    
    html = html.replace('<td style="padding: 15px; background: #f8f9fa; border: none;">إجمالي الدائن</td>', '<td style="padding: 15px; background: #f8f9fa; color: #333; font-weight: bold; border: none;">إجمالي الدائن</td>');
    
    // Fix light blue (btn-view) button for Print Account Statement
    html = html.replace('<button class="action-btn btn-view" style="margin-right:auto; margin-left:20px; font-size:1.2rem; padding:5px 15px;" onclick="printContactStatement()">طباعة كشف الحساب 🖨️</button>', '<button class="btn-primary" style="margin-right:auto; margin-left:20px; font-size:1.2rem; padding:10px 20px; box-shadow: 0 4px 6px rgba(0,0,0,0.1);" onclick="printContactStatement()">🖨️ طباعة كشف الحساب</button>');

    html = html.replace('<button class="action-btn btn-view" style="margin-right:auto; margin-left:20px; font-size:1.2rem; padding:5px 15px;" onclick="printContactStatement()">🖨️ طباعة كشف الحساب</button>', '<button class="btn-primary" style="margin-right:auto; margin-left:20px; font-size:1.2rem; padding:10px 20px; box-shadow: 0 4px 6px rgba(0,0,0,0.1);" onclick="printContactStatement()">🖨️ طباعة كشف الحساب</button>');
    
    // Some instances might have emojis swapped or absent
    html = html.replace(/<button class="action-btn btn-view" style="margin-right:auto; margin-left:20px; font-size:1.2rem; padding:5px 15px;" onclick="printContactStatement\(\)">.*?طباعة كشف الحساب.*?<\/button>/, '<button class="btn-primary" style="margin-right:auto; margin-left:20px; font-size:1.2rem; padding:10px 20px; box-shadow: 0 4px 6px rgba(0,0,0,0.1);" onclick="printContactStatement()">🖨️ طباعة كشف الحساب</button>');
    
    // Also, if there's any other text with color: #fff on light backgrounds that I should fix globally?
    // In index.html, let's fix the totals boxes inside statement
    fs.writeFileSync('index.html', html, 'utf8');
    console.log('Fixed colors in index.html');
}

if (fs.existsSync('style.css')) {
    let css = fs.readFileSync('style.css', 'utf8');
    // If the user hates the light blue of .btn-view overall:
    // .btn-view { color: #3498db; background: rgba(52, 152, 219, 0.15); border: 1px solid rgba(52, 152, 219, 0.3); }
    // Let's make it darker! #1d6fa5
    css = css.replace(/color: #3498db;/g, 'color: #1d6fa5;');
    css = css.replace(/rgba\(52, 152, 219, 0\.15\)/g, 'rgba(29, 111, 165, 0.15)');
    fs.writeFileSync('style.css', css, 'utf8');
    console.log('Fixed btn-view color in style.css');
}

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