const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

const SERVICES_DIR = __dirname;

// 需要修复的目录（大写 -> 小写）
const dirsToFix = [
  { from: 'AI', to: 'ai' },
  { from: 'Analytics', to: 'analytics' },
  { from: 'Core', to: 'core' },
  { from: 'Logistics', to: 'logistics' },
  { from: 'Product', to: 'product' },
  { from: 'Business', to: 'business' },
  { from: 'Infra', to: 'infra' }
];

console.log('Renaming directories via temporary names...\n');

dirsToFix.forEach(({ from, to }) => {
  const fromPath = path.join(SERVICES_DIR, from);
  const toPath = path.join(SERVICES_DIR, to);
  const tempPath = path.join(SERVICES_DIR, `${from}_TEMP_12345`);
  
  if (!fs.existsSync(fromPath)) {
    console.log(`Source does not exist: ${from}`);
    return;
  }
  
  if (fs.existsSync(toPath) && from !== to) {
    console.log(`Target already exists: ${to}`);
    return;
  }
  
  try {
    // 步骤1: 重命名为临时名称
    console.log(`Step 1: ${from} -> ${from}_TEMP_12345`);
    fs.renameSync(fromPath, tempPath);
    
    // 步骤2: 重命名为小写名称
    console.log(`Step 2: ${from}_TEMP_12345 -> ${to}`);
    fs.renameSync(tempPath, toPath);
    
    console.log(`✓ Successfully renamed: ${from} -> ${to}\n`);
  } catch (error) {
    console.error(`✗ Failed to rename ${from}: ${error.message}\n`);
  }
});

console.log('Done!');
