import { Hype } from 'hype'
import { initApps, startApp, stopApp } from './apps'

const app = new Hype()

console.log('🐾 Toes!')
initApps()

app.post('/apps/:app/start', c => {
  const app = c.req.param('app')
  if (!app) return render404(c)

  startApp(app)
  return c.redirect('/')
})

app.post('/apps/:app/restart', c => {
  const app = c.req.param('app')
  if (!app) return render404(c)

  stopApp(app)
  startApp(app)
  return c.redirect('/')
})

app.post('/apps/:app/stop', c => {
  const app = c.req.param('app')
  if (!app) return render404(c)

  stopApp(app)
  return c.redirect('/')
})

const render404 = (c: any) =>
  c.text('404 Not Found', { status: 404 })

export default app.defaults
