const fs = require('fs');

if (fs.existsSync('index.html')) {
    let html = fs.readFileSync('index.html', 'utf8');
    
    // Add cache busters
    const timestamp = Date.now();
    html = html.replace(/href="style\.css(\?v=\d+)?"/g, `href="style.css?v=${timestamp}"`);
    html = html.replace(/src="app_final\.js(\?v=\d+)?"/g, `src="app_final.js?v=${timestamp}"`);
    html = html.replace(/src="vouchers_returns\.js(\?v=\d+)?"/g, `src="vouchers_returns.js?v=${timestamp}"`);
    html = html.replace(/src="subcontracts\.js(\?v=\d+)?"/g, `src="subcontracts.js?v=${timestamp}"`);
    html = html.replace(/src="sales_purchases_module\.js(\?v=\d+)?"/g, `src="sales_purchases_module.js?v=${timestamp}"`);
    
    // Also hardcode a style inline for the statement just to be 100% sure it works regardless of caching
    // The credit text is inside app_final.js: `<td style="color:var(--success)">${r.credit ? ...`
    
    fs.writeFileSync('index.html', html, 'utf8');
    console.log('Added cache busters to index.html');
}

// Ensure app_final.js also uses explicit color just in case var(--success) fails
if (fs.existsSync('app_final.js')) {
    let js = fs.readFileSync('app_final.js', 'utf8');
    // Replace <td style="color:var(--success)"> with hardcoded dark green for statement
    js = js.replace(/<td style="color:var\(--success\)">\$\{r\.credit/g, '<td style="color:#008a45; font-weight:bold;">${r.credit');
    fs.writeFileSync('app_final.js', js, 'utf8');
    console.log('Hardcoded dark green in app_final.js');
}
