const fs = require('fs');
let js = fs.readFileSync('app_final.js', 'utf8');

const regex = /document\.getElementById\('btn-login'\)\.addEventListener\('click', async \(\) => \{\s*const user = document\.getElementById\('login-username'\)\.value;\s*const pass = document\.getElementById\('login-password'\)\.value;\s*const error = document\.getElementById\('login-error'\);\s*const res = await window\.DB\.login\(user, pass\);\s*if \(res\) \{\s*currentUser = res;\s*sessionStorage\.setItem\('currentUser', JSON\.stringify\(res\)\);\s*\} else \{\s*window\.addJournalLine\(\); window\.addJournalLine\(\);\s*\}(?:.|\n)*?\/\/ Listeners\s*document\.getElementById\('btn-login'\)\.addEventListener\('click', async \(\) => \{\s*const user = document\.getElementById\('login-username'\)\.value;\s*const pass = document\.getElementById\('login-password'\)\.value;\s*const error = document\.getElementById\('login-error'\);\s*const res = await window\.DB\.login\(user, pass\);\s*if \(res\) \{\s*currentUser = res;\s*sessionStorage\.setItem\('currentUser', JSON\.stringify\(res\)\);\s*loginScreen\.classList\.remove\('active'\);\s*appScreen\.classList\.add\('active'\);\s*setupUserUI\(\);\s*await loadData\(\);\s*\} else \{\s*error\.innerText = '.*?';\s*\}\s*\}\);\s*document\.getElementById\('btn-logout'\)\.addEventListener\('click', \(\) => \{\s*currentUser = null;\s*sessionStorage\.removeItem\('currentUser'\);\s*appScreen\.classList\.remove\('active'\);\s*loginScreen\.classList\.add\('active'\);\s*\}\);\s*navItems\.forEach\(item => \{\s*item\.addEventListener\('click', \(e\) => \{\s*if\(e\.target\.id === 'btn-logout'\) return;\s*navItems\.forEach\(n => n\.classList\.remove\('active'\)\);\s*e\.target\.classList\.add\('active'\);\s*views\.forEach\(v => v\.classList\.remove\('active'\)\);\s*document\.getElementById\(\`view-\$\{e\.target\.dataset\.view\}\`\)\.classList\.add\('active'\);\s*if\(e\.target\.dataset\.view === 'project-reports'\) renderProjectReports\(\);\s*if\(e\.target\.dataset\.view === 'subcontracts'\) window\.renderSubcontracts\(\);\s*if\(e\.target\.dataset\.view === 'subcontract-abstracts'\) window\.renderAbstracts\(\);/;

const replacement = `document.getElementById('btn-login').addEventListener('click', async () => {
  const user = document.getElementById('login-username').value;
  const pass = document.getElementById('login-password').value;
  const error = document.getElementById('login-error');

  const res = await window.DB.login(user, pass);
  if (res) {
    currentUser = res;
    sessionStorage.setItem('currentUser', JSON.stringify(res));
    loginScreen.classList.remove('active');
    appScreen.classList.add('active');
    setupUserUI();
    await loadData();
  } else {
    error.innerText = 'بيانات الدخول غير صحيحة';
  }
});

document.getElementById('btn-logout').addEventListener('click', () => {
  currentUser = null;
  sessionStorage.removeItem('currentUser');
  appScreen.classList.remove('active');
  loginScreen.classList.add('active');
});

navItems.forEach(item => {
  item.addEventListener('click', (e) => {
    if(e.target.id === 'btn-logout') return;
    navItems.forEach(n => n.classList.remove('active'));
    e.target.classList.add('active');
    views.forEach(v => v.classList.remove('active'));
    document.getElementById(\`view-\${e.target.dataset.view}\`).classList.add('active');
    if(e.target.dataset.view === 'project-reports') renderProjectReports();
    if(e.target.dataset.view === 'subcontracts') window.renderSubcontracts();
    if(e.target.dataset.view === 'subcontract-abstracts') window.renderAbstracts();
    if(e.target.dataset.view === 'client-abstracts' && typeof window.renderClientAbstracts === 'function') window.renderClientAbstracts();`;

const newJs = js.replace(regex, replacement);

fs.writeFileSync('app_final.js', newJs, 'utf8');
console.log('Fixed app_final.js!');
