const fs = require('fs');
const path = require('path');

const projectRoot = path.resolve(__dirname, '..');
const layoutFilePath = path.join(projectRoot, 'frontend-web', 'src', 'pages', 'Layout.jsx');

const oldString = `    queryKey: ['current-user-layout'],`;
const newString = `    queryKey: ['current-user'],`;

try {
  const content = fs.readFileSync(layoutFilePath, 'utf8');

  if (content.includes(oldString)) {
    const newContent = content.replace(oldString, newString);
    fs.writeFileSync(layoutFilePath, newContent, 'utf8');
    console.log('✅ Successfully patched queryKey in frontend-web/src/pages/Layout.jsx');
  } else if (content.includes(newString)) {
    console.log('ℹ️ queryKey in Layout.jsx is already patched. Skipping.');
  } else {
    console.error('❌ Patching failed: Could not find the expected queryKey in Layout.jsx.');
    process.exit(1);
  }
} catch (error) {
  console.error('❌ An error occurred during patching Layout.jsx queryKey:', error);
  process.exit(1);
}
