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

// 服务目录
const SERVICES_DIR = __dirname;

// 递归将目录重命名为小写
const forceLowercaseDirectory = (dirPath) => {
  const items = fs.readdirSync(dirPath);
  
  items.forEach(item => {
    const itemPath = path.join(dirPath, item);
    const stat = fs.statSync(itemPath);
    
    if (stat.isDirectory()) {
      // 跳过特殊目录
      if (item === '__tests__' || item === 'node_modules') {
        return;
      }
      
      // 将目录名转换为小写
      const lowercaseName = item.toLowerCase();
      const lowercasePath = path.join(dirPath, lowercaseName);
      
      if (item !== lowercaseName) {
        if (fs.existsSync(lowercasePath)) {
          console.log(`Skipped directory rename: ${item} -> ${lowercaseName} (lowercase directory already exists)`);
        } else {
          // 先重命名为临时名称
          const tempName = `${item}_temp_${Date.now()}`;
          const tempPath = path.join(dirPath, tempName);
          
          try {
            fs.renameSync(itemPath, tempPath);
            console.log(`Renamed directory to temp: ${item} -> ${tempName}`);
            
            // 再重命名为小写
            fs.renameSync(tempPath, lowercasePath);
            console.log(`Renamed directory to lowercase: ${tempName} -> ${lowercaseName}`);
            
            // 递归处理子目录
            forceLowercaseDirectory(lowercasePath);
          } catch (error) {
            console.error(`Error renaming directory ${item}: ${error.message}`);
          }
        }
      } else {
        // 目录名已经是小写，递归处理子目录
        forceLowercaseDirectory(itemPath);
      }
    }
  });
};

// 运行目录大小写强制转换
const runForceLowercase = () => {
  console.log('Starting force lowercase directory conversion...');
  forceLowercaseDirectory(SERVICES_DIR);
  console.log('\nForce lowercase directory conversion completed!');
};

// 运行转换
runForceLowercase();
