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

const SRC_DIR = path.join(__dirname, '..');

// 修复文件中的domains路径导入
const fixDomainImports = (filePath) => {
  try {
    let content = fs.readFileSync(filePath, 'utf8');
    let changed = false;
    
    // 修复从services目录到domains目录的导入路径
    const patterns = [
      // from 语句，使用单引号
      `from '../../domains/`,
      `from "../../domains/`,
      // require 语句，使用单引号
      `require('../../domains/`,
      `require("../../domains/`
    ];
    
    patterns.forEach(pattern => {
      if (content.includes(pattern)) {
        const replacement = pattern.replace('../domains/', '../../domains/');
        content = content.split(pattern).join(replacement);
        changed = true;
      }
    });
    
    if (changed) {
      fs.writeFileSync(filePath, content, 'utf8');
      console.log(`Fixed domain imports in: ${filePath}`);
    }
  } catch (error) {
    console.error(`Error fixing domain imports in ${filePath}: ${error.message}`);
  }
};

// 递归修复目录中的所有文件
const fixImportsRecursive = (dirPath) => {
  const items = fs.readdirSync(dirPath);
  
  items.forEach(item => {
    const itemPath = path.join(dirPath, item);
    const stat = fs.statSync(itemPath);
    
    if (stat.isDirectory() && !item.startsWith('__') && item !== 'node_modules') {
      fixImportsRecursive(itemPath);
    } else if (stat.isFile() && (item.endsWith('.ts') || item.endsWith('.js')) && !item.endsWith('.d.ts')) {
      fixDomainImports(itemPath);
    }
  });
};

// 主函数
const main = () => {
  console.log('=== Fixing Domain Imports ===\n');
  
  // 修复所有TypeScript和JavaScript文件中的domains导入路径
  fixImportsRecursive(SRC_DIR);
  
  console.log('\n=== Fixing Domain Imports Completed ===');
};

// 运行主函数
main();
