const fs = require('fs');

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

const oldListenerRegex = /document\.getElementById\('user-role'\)\.addEventListener\('change',\s*\(\w*\)\s*=>\s*\{[\s\S]*?\}\);/;
const newListener = `document.getElementById('user-role').addEventListener('change', (e) => {
  const role = e.target.value;
  const wh = document.getElementById('user-warehouse-group');
  const proj = document.getElementById('user-project-group');
  const customPerms = document.getElementById('custom-permissions-group');
  
  if (role === 'مدير عام') {
    wh.style.display = 'none'; 
    proj.style.display = 'none';
    customPerms.style.display = 'none';
  } else {
    // Show permissions, warehouses, and projects for ALL other roles
    wh.style.display = 'block'; 
    proj.style.display = 'block';
    customPerms.style.display = 'block';
  }
});`;

if (oldListenerRegex.test(appFinal)) {
    appFinal = appFinal.replace(oldListenerRegex, newListener);
    fs.writeFileSync('d:/برنامج مخازن/app_final.js', appFinal, 'utf8');
    console.log('Fixed user-role listener in app_final.js');
} else {
    console.log('Failed to find user-role listener in app_final.js');
}

// 2. Fix entities_module.js to remove role === 'موظف مخصص' restriction when saving
let entities = fs.readFileSync('d:/برنامج مخازن/entities_module.js', 'utf8');

const oldExtract = `      if (role === 'موظف مخصص') {
        document.querySelectorAll('.perm-module:checked').forEach(cb => allowed_modules.push(cb.value));
        document.querySelectorAll('.perm-warehouse:checked').forEach(cb => allowed_warehouses.push(parseInt(cb.value)));
        document.querySelectorAll('.perm-project:checked').forEach(cb => allowed_projects.push(parseInt(cb.value)));
      }`;
const newExtract = `      
      document.querySelectorAll('.perm-module:checked').forEach(cb => allowed_modules.push(cb.value));
      document.querySelectorAll('.perm-warehouse:checked').forEach(cb => allowed_warehouses.push(parseInt(cb.value)));
      document.querySelectorAll('.perm-project:checked').forEach(cb => allowed_projects.push(parseInt(cb.value)));
`;

if (entities.includes(oldExtract)) {
    entities = entities.replace(oldExtract, newExtract);
    fs.writeFileSync('d:/برنامج مخازن/entities_module.js', entities, 'utf8');
    console.log('Fixed entities_module.js permissions extraction.');
} else {
    // In case the indentation is different, we can use a more flexible regex
    const regex = /if\s*\(role\s*===\s*'موظف مخصص'\)\s*\{[\s\S]*?allowed_projects\.push\(parseInt\(cb\.value\)\)\);\s*\}/;
    if (regex.test(entities)) {
        entities = entities.replace(regex, newExtract);
        fs.writeFileSync('d:/برنامج مخازن/entities_module.js', entities, 'utf8');
        console.log('Fixed entities_module.js permissions extraction (using regex).');
    } else {
        console.log('Could not find role === "موظف مخصص" block in entities_module.js. It might already be removed.');
    }
}
