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

const REQUIRED_DEPENDENCIES = [
  '@babel/core',
  '@babel/generator',
  '@babel/parser',
  '@babel/preset-env',
  '@babel/preset-react',
  '@babel/traverse',
  '@babel/types',
  'axios',
  'chalk',
  'commander',
  'form-data',
  'inquirer',
  'keytar',
  'ora',
  'eslint',
  'open',
  'postcss',
  'postcss-selector-parser',
]

function checkDependencies() {
  const missingDeps = []
  const nodeModulesPath = path.join(__dirname, '..', '..', 'node_modules')

  for (const dep of REQUIRED_DEPENDENCIES) {
    const depPath = path.join(nodeModulesPath, dep)
    if (!fs.existsSync(depPath)) {
      missingDeps.push(dep)
    }
  }

  if (missingDeps.length > 0) {
    console.error(chalk.red('\n✗ Missing required dependencies:'))
    missingDeps.forEach((dep) => {
      console.error(chalk.red(`  - ${dep}`))
    })
    console.error(chalk.yellow('\nPlease run: npm install'))
    console.error(chalk.yellow('Or: npm install --save ' + missingDeps.join(' ')))
    return false
  }

  return true
}

module.exports = {
  checkDependencies,
  REQUIRED_DEPENDENCIES,
}

