#!/usr/bin/env janet

(use joy)
(import cipher)

(def args (dyn :args))

(def usage
  "Usage"
``` [action]

  Actions:
    help                                     - Print this usage information
    new <project-name>                       - Create a new folder with the name of your project
    create <db|route|table|migration> <name> - Generate a new thing
    migrate                                  - Migrates the database
    rollback                                 - Rolls back the most recent migration
    server                                   - Serves the app
    secret                                   - Prints out new key for session encryption
    version                                  - Print the current version
```)

(def action (get args 1))
(def options (drop 2 args))

(def actions ["create" "help" "new" "migrate" "rollback" "server" "watch" "version" "secret"])

(if (and (nil? (find-index |(= action $) actions))
         (not (nil? action)))
  (do
    (print "'" action "' is not a supported action.\n")
    (print "joy" usage)
    (os/exit 1)))

(if (or (nil? action)
        (empty? action))
  (print "joy" usage)
  (case action
    "create" (apply generate options)
    "help" (print "joy" usage)
    "new" (generate "project" ;options)
    "migrate" (db/migrate (env :database-url))
    "rollback" (db/rollback (env :database-url))
    "server" (os/shell "jpm run server")
    "watch" (os/shell "jpm run watch")
    "version" (print version)
    "secret" (print (cipher/encryption-key))))
