with open("app/static/index.html", "r", encoding="utf-8") as f:
    content = f.read()

# Add link in the nav menu
nav_menu_pos = content.find('<a href="/dashboard" class="erp-navlink active">')
if nav_menu_pos != -1:
    link_html = """
        <a href="/storage/audit_report.html" class="erp-navlink" id="test-report-link" style="display: none;" target="_blank">
          <span>🛡️</span> Test Report
        </a>
"""
    content = content[:nav_menu_pos] + link_html.lstrip() + content[nav_menu_pos:]

# Add JS logic in DOMContentLoaded
js_pos = content.find("if (nameSpan) {")
if js_pos != -1:
    js_logic = """
          const testReportLink = document.getElementById('test-report-link');
          if (testReportLink && user.role === 'superadmin') {
            testReportLink.style.display = 'flex';
          }
"""
    content = content[:js_pos] + js_logic.lstrip() + content[js_pos:]

with open("app/static/index.html", "w", encoding="utf-8") as f:
    f.write(content)
