// 다크모드 일괄 수정 스크립트
// Node.js로 실행: node scripts/fix-dark-mode.js

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

// 수정 규칙 정의
const replacements = [
  // 1. bg-white
  {
    find: /className="([^"]*?)bg-white([^-])/g,
    replace: 'className="$1bg-white dark:bg-card$2'
  },
  // 2. bg-gray-50 (not followed by /)
  {
    find: /className="([^"]*?)bg-gray-50([^-/])/g,
    replace: 'className="$1bg-gray-50 dark:bg-muted$2'
  },
  // 3. bg-gray-100
  {
    find: /className="([^"]*?)bg-gray-100([^-])/g,
    replace: 'className="$1bg-gray-100 dark:bg-gray-700$2'
  },
  // 4. text-gray-700
  {
    find: /className="([^"]*?)text-gray-700([^-])/g,
    replace: 'className="$1text-gray-700 dark:text-gray-300$2'
  },
  // 5. text-gray-900
  {
    find: /className="([^"]*?)text-gray-900([^-])/g,
    replace: 'className="$1text-gray-900 dark:text-gray-100$2'
  },
  // 6. border-gray-200
  {
    find: /className="([^"]*?)border-gray-200([^-])/g,
    replace: 'className="$1border-gray-200 dark:border-gray-700$2'
  },
  // 7. border-purple-200
  {
    find: /className="([^"]*?)border-purple-200([^-])/g,
    replace: 'className="$1border-purple-200 dark:border-purple-800$2'
  },
  // 8. bg-purple-50
  {
    find: /className="([^"]*?)bg-purple-50([^-])/g,
    replace: 'className="$1bg-purple-50 dark:bg-purple-900/30$2'
  },
  // 9. bg-purple-100
  {
    find: /className="([^"]*?)bg-purple-100([^-])/g,
    replace: 'className="$1bg-purple-100 dark:bg-purple-900/40$2'
  },
  // 10. text-purple-700
  {
    find: /className="([^"]*?)text-purple-700([^-])/g,
    replace: 'className="$1text-purple-700 dark:text-purple-300$2'
  },
  // 11. text-purple-900
  {
    find: /className="([^"]*?)text-purple-900([^-])/g,
    replace: 'className="$1text-purple-900 dark:text-purple-100$2'
  },
  // 12. bg-blue-50
  {
    find: /className="([^"]*?)bg-blue-50([^-])/g,
    replace: 'className="$1bg-blue-50 dark:bg-blue-900/30$2'
  },
  // 13. bg-blue-100
  {
    find: /className="([^"]*?)bg-blue-100([^-])/g,
    replace: 'className="$1bg-blue-100 dark:bg-blue-900/40$2'
  },
  // 14. text-blue-700
  {
    find: /className="([^"]*?)text-blue-700([^-])/g,
    replace: 'className="$1text-blue-700 dark:text-blue-300$2'
  },
  // 15. border-blue-200
  {
    find: /className="([^"]*?)border-blue-200([^-])/g,
    replace: 'className="$1border-blue-200 dark:border-blue-800$2'
  },
  // 16. bg-green-50
  {
    find: /className="([^"]*?)bg-green-50([^-])/g,
    replace: 'className="$1bg-green-50 dark:bg-green-900/30$2'
  },
  // 17. bg-green-100
  {
    find: /className="([^"]*?)bg-green-100([^-])/g,
    replace: 'className="$1bg-green-100 dark:bg-green-900/40$2'
  },
  // 18. text-green-700
  {
    find: /className="([^"]*?)text-green-700([^-])/g,
    replace: 'className="$1text-green-700 dark:text-green-300$2'
  },
  // 19. border-green-200
  {
    find: /className="([^"]*?)border-green-200([^-])/g,
    replace: 'className="$1border-green-200 dark:border-green-800$2'
  },
  // 20. bg-red-50
  {
    find: /className="([^"]*?)bg-red-50([^-])/g,
    replace: 'className="$1bg-red-50 dark:bg-red-900/30$2'
  },
  // 21. bg-red-100
  {
    find: /className="([^"]*?)bg-red-100([^-])/g,
    replace: 'className="$1bg-red-100 dark:bg-red-900/40$2'
  },
  // 22. text-red-700
  {
    find: /className="([^"]*?)text-red-700([^-])/g,
    replace: 'className="$1text-red-700 dark:text-red-300$2'
  },
  // 23. border-red-200
  {
    find: /className="([^"]*?)border-red-200([^-])/g,
    replace: 'className="$1border-red-200 dark:border-red-800$2'
  }
];

// 파일 처리 함수
function processFile(filePath) {
  console.log(`Processing: ${filePath}`);
  
  let content = fs.readFileSync(filePath, 'utf8');
  let modified = false;
  
  replacements.forEach(({ find, replace }) => {
    const newContent = content.replace(find, replace);
    if (newContent !== content) {
      content = newContent;
      modified = true;
    }
  });
  
  if (modified) {
    fs.writeFileSync(filePath, content, 'utf8');
    console.log(`✓ Updated: ${filePath}`);
  } else {
    console.log(`- No changes: ${filePath}`);
  }
}

// 디렉토리 재귀 탐색
function processDirectory(dirPath) {
  const files = fs.readdirSync(dirPath);
  
  files.forEach(file => {
    const filePath = path.join(dirPath, file);
    const stat = fs.statSync(filePath);
    
    if (stat.isDirectory()) {
      // 제외할 디렉토리
      if (!['node_modules', '.git', '.next', 'dist', 'build'].includes(file)) {
        processDirectory(filePath);
      }
    } else if (file.endsWith('Management.tsx') || file.endsWith('.tsx')) {
      // .tsx 파일만 처리
      processFile(filePath);
    }
  });
}

// 실행
const componentsPath = path.join(__dirname, '..', 'components');
console.log('Starting dark mode fix...\n');
processDirectory(componentsPath);
console.log('\n✓ Dark mode fix completed!');
