const fs = require('fs');

let appFinal = fs.readFileSync('d:/برنامج مخازن/app_final.js', 'utf8');

// Find the function setupUserUI() block and replace it
const setupUserUIStart = appFinal.indexOf('function setupUserUI() {');

// Find the end of setupUserUI block
// It ends just before `// --- ACCOUNTING & JOURNALS LOGIC ---`
const setupUserUIEnd = appFinal.indexOf('// --- ACCOUNTING & JOURNALS LOGIC ---');

if (setupUserUIStart !== -1 && setupUserUIEnd !== -1) {
    const newSetupUserUI = `function setupUserUI() {
    document.getElementById('current-user-name').innerText = currentUser.username;
    document.getElementById('current-user-role').innerText = currentUser.role;
    
    const allNavs = ['nav-users', 'nav-warehouses', 'nav-decisions', 'nav-contacts', 'nav-units', 'nav-items', 
'nav-item-card', 'nav-projects', 'nav-project-reports', 'nav-balances', 'nav-transactions', 'nav-accounting', 
'nav-sales', 'nav-purchases', 'nav-vouchers', 'nav-settlements', 'nav-sales-returns', 'nav-purchases-returns', 
'nav-project-costs', 'nav-subcontracts', 'nav-subcontract-abstracts', 'nav-client-abstracts'];
    (allNavs || []).forEach(nav => {
      const el = document.getElementById(nav);
      if(el) el.style.display = 'none';
    });
    document.getElementById('btn-add-item-modal').style.display = 'none';
    
    if (currentUser.role === 'مدير عام') {
      (allNavs || []).forEach(nav => {
        const el = document.getElementById(nav);
        if(el) el.style.display = 'block';
      });
      document.getElementById('btn-add-item-modal').style.display = 'inline-block';
      document.getElementById('current-user-warehouse').innerText = 'كل المخازن';
    } else {
      const mods = currentUser.allowed_modules || [];
      if(mods.includes('users')) document.getElementById('nav-users').style.display = 'block';
      if(mods.includes('warehouses')) document.getElementById('nav-warehouses').style.display = 'block';
      if(mods.includes('contacts')) document.getElementById('nav-contacts').style.display = 'block';
      if(mods.includes('units')) document.getElementById('nav-units').style.display = 'block';
      
      if(mods.includes('items')) {
        document.getElementById('nav-items').style.display = 'block';
        document.getElementById('nav-item-card').style.display = 'block';
        document.getElementById('btn-add-item-modal').style.display = 'inline-block';
      }
      
      if(mods.includes('projects')) {
          document.getElementById('nav-projects').style.display = 'block';
          document.getElementById('nav-project-costs').style.display = 'block';
          document.getElementById('nav-client-abstracts').style.display = 'block';
      }
      if(mods.includes('project-reports')) document.getElementById('nav-project-reports').style.display = 'block';
      if(mods.includes('balances')) document.getElementById('nav-balances').style.display = 'block';
      if(mods.includes('transactions')) {
        document.getElementById('nav-transactions').style.display = 'block';
        document.getElementById('nav-decisions').style.display = 'block';
      }
      
      if(mods.includes('sales')) {
        document.getElementById('nav-sales').style.display = 'block';
        document.getElementById('nav-sales-returns').style.display = 'block';
      }
      
      if(mods.includes('purchases')) {
        document.getElementById('nav-purchases').style.display = 'block';
        document.getElementById('nav-purchases-returns').style.display = 'block';
      }
      
      if(mods.includes('accounting')) {
        document.getElementById('nav-accounting').style.display = 'block';
      }
      
      if(mods.includes('contractors')) {
        document.getElementById('nav-subcontracts').style.display = 'block';
        document.getElementById('nav-subcontract-abstracts').style.display = 'block';
      }
      
      if(mods.includes('settlements')) {
        document.getElementById('nav-settlements').style.display = 'block';
      }
      
      if(mods.includes('vouchers')) {
        document.getElementById('nav-vouchers').style.display = 'block';
      }
      
      let whText = '';
      if(currentUser.allowed_warehouses && currentUser.allowed_warehouses.length > 0) whText = currentUser.allowed_warehouses.length + ' مخزن';
      else whText = 'لا يوجد مخازن';
      document.getElementById('current-user-warehouse').innerText = whText;
    }
}
`;
    appFinal = appFinal.substring(0, setupUserUIStart) + newSetupUserUI + '\n' + appFinal.substring(setupUserUIEnd);
    fs.writeFileSync('d:/برنامج مخازن/app_final.js', appFinal, 'utf8');
    console.log('Successfully replaced setupUserUI in app_final.js');
} else {
    console.log('Failed to find setupUserUI boundaries.');
}
