#!/usr/bin/env node

const console = require('console')
const fs = require('fs')
const path = require('path')
const readline = require('readline')

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})
console.log('\n[1] Car Repair Parking Managemnt System')
console.log('[2]Employee Payroll Management System')
console.log('[3]Employee Payroll Management System Elysee')
console.log('[4]middleware')
console.log('[5] Parking Management System')
console.log('[6]Supply Chain Managemnt System')
console.log('[7]Supply Chain Management System Elysee')
 console.log('[8] School fee Management System')
 console.log('[9]Stock Hub Management System')
 console.log('[10]Stock Invetory Management System')
 console.log('[11]Stock Management System')
 console.log('[12]Sales Record Management System')
 console.log('[13]Sales Record Management System Elysee')
 console.log('[14]userController.js')
 



rl.question('Pick (1-14): ', (answer) => {

  const projects = {
    '1':'crpms',
    '2':'EPMS',
    '3':'EPMS(Ely)',
    '4':'middleware',
    '5': 'parking managementt system',
    '6':'SCMS',
    
    '7':'SCMS(Ely)',
    '8': 'sfms-app',
    '9':'SHMS(Ely)',
    '10':'sims',
    '11': 'SMS-',
    '12':'SRMS',
    '13':'SRMS(Ely)',
    '14':'user',
    
     
  }

  const chosen = projects[answer]

  if (!chosen) {
    console.log('Invalid choice!')
    rl.close()
    return
  }

  const src = path.join(__dirname, 'projects', chosen)
  const dest = path.join(process.cwd(), chosen)

  console.log(`\nDownloading ${chosen}...`)
  copyFolder(src, dest)

  console.log(`\nDone! Now run:`)
  console.log(`\n--- FRONTEND ---`)
  console.log(`   cd "${chosen}/frontend"`)
  console.log(`   npm install`)
  console.log(`   npm run dev`)
  console.log(`\n--- BACKEND ---`)
  console.log(`   cd "${chosen}/backend"`)
  console.log(`   npm install`)
  console.log(`   npm start\n`)

  rl.close()
})

function copyFolder(src, dest) {
  fs.mkdirSync(dest, { recursive: true })
  const entries = fs.readdirSync(src, { withFileTypes: true })

  for (let entry of entries) {
    const srcPath = path.join(src, entry.name)
    const destPath = path.join(dest, entry.name)

    if (entry.name === 'node_modules') continue

    if (entry.isDirectory()) {
      copyFolder(srcPath, destPath)
    } else {
      fs.copyFileSync(srcPath, destPath)
    }
  }
}