#!/usr/bin/env python

"""Yhat Command Line Tools.

Usage:
  yhat-cli config [--reset]
  yhat-cli models [--admin]
  yhat-cli model <modelname>
  yhat-cli (-h | --help)
  yhat-cli (-v | --version)

Arguments:
  modelname        The name of a model.

Options:
  -h --help        Show this screen.
  -v --version     Show version.
  --reset          Reset the config.
  --admin          Get all models (admin users only).

"""

from docopt import docopt
from yhat import credentials
from elastictabstops import Table
from yhat import models


if __name__ == "__main__":
    arguments = docopt(__doc__, version='Yhat Command Line Tools 1.1.0')
    if arguments['config']:
        if arguments['--reset'] or credentials.has() is False:
            # save the user's credentials in a dotfile
            credentials.setup()
        else:
            # print out the user's saved config
            creds_table = []
            for key, value in credentials.read().items():
                creds_table.append([str(key), str(value)])
            print Table(creds_table).to_spaces()
    elif arguments['models'] and credentials.has() is True:
        m = models.get(admin=arguments['--admin'])
        print models.table(m, admin=arguments['--admin'])
    elif (arguments['model'] and arguments['<modelname>'] is not None
          and credentials.has() is True):
        m = models.get(modelname=arguments['<modelname>'])
        print models.table(m)
    else:
        credentials.setup()
