#!/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]middleware')
console.log('[3] Parking Management System')
 console.log('[4] School fee Management System')
 console.log('[5]Stock Management System')
 console.log('[6]Stock Record Management System')
 console.log('[7]userController.js')
 console.log('[8] yhh ')
 



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

  const projects = {
    '1':'crpms',
    '2':'middleware',
    '3': 'parking managementt system',
    '4': 'sfms-app',
    '5': 'SMS-',
    '6':'SRMS',
    '7':'user',
    '8':'yhh',
     
  }

  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)
    }
  }
}