import evaluate from '../src/area-tools/evaluate'
import nomnom from 'nomnom'
import fs from 'graceful-fs'
import compute from '../src/area-tools/compute'
import get from 'lodash/object/get'
import loadArea from './lib/load-area'
import yaml from 'js-yaml'

const checkAgainstArea = ({courses, overrides}, args) => areaData => {
	if (args.path) {
		const result = compute(
			get(areaData, args.path), {
				path: [areaData.type, areaData.name].concat(args.path.split('.')),
				courses, overrides})
		console.log(result)
		return
	}

	const result = evaluate({courses, overrides}, areaData)

	if (args.json) {
		console.log(JSON.stringify(result, null, 2))
	}
	else if (args.yaml) {
		console.log(yaml.safeDump(result))
	}
	else if (args.prose) {
		console.log('not implemented')
		// console.log(proseify(result))
	}
	else if (args.summary) {
		console.log('not implemented')
		// console.log(summarize(result))
	}

	if (!result.computed) {
		process.exit(1)
	}
}

function run({courses, overrides, areas}, args) {
	Promise.all(areas.map(loadArea)).then(areaDatœ => {
		for (const area of areaDatœ) {
			checkAgainstArea({courses, overrides}, args)(area)
		}
	})
}

export function cli() {
	const args = nomnom()
		.option('json', {
			flag: true,
			help: 'print raw json output',
		})
		.option('yaml', {
			flag: true,
			help: 'print yaml-formatted json output',
		})
		.option('prose', {
			flag: true,
			help: 'print prose output',
		})
		.option('summary', {
			flag: true,
			help: 'print summarized output',
		})
		.option('status', {
			flag: true,
			help: 'no output; only use exit code',
		})
		.option('path', {
			type: 'text',
			help: 'change the root of the evaluation',
		})
		.option('studentFile', {
			required: true,
			metavar: 'FILE',
			help: 'The file to process',
			position: 0,
		})
		.parse()

	run(JSON.parse(fs.readFileSync(args.studentFile, 'utf-8')), args)
}

@@ -249,16 +249,624 @@
 js-yaml'
+%0Aimport isRequirementName from '../src/area-tools/is-requirement-name'%0Aimport pairs from 'lodash/object/pairs'%0Aimport map from 'lodash/collection/map'%0Aimport filter from 'lodash/collection/filter'%0Aimport repeat from 'lodash/string/repeat'%0A%0A%0Afunction summarize(requirement, name, path, depth=0) %7B%0A%09const subReqs = filter(pairs(requirement), (%5Bk, _%5D) =%3E isRequirementName(k))%0A%0A%09let prose = ''%0A%09if (subReqs.length) %7B%0A%09%09prose = '%5Cn' + map(subReqs, (%5Bk, v%5D) =%3E %7B%0A%09%09%09return summarize(v, k, path.concat(k), depth + 1)%0A%09%09%7D).join('%5Cn')%0A%09%7D%0A%0A%09return %60$%7Brepeat(' ', depth * 2)%7D$%7Bname%7D: $%7Brequirement.computed%7D$%7Bprose%7D%60%0A%7D
 %0A%0Aconst 
@@ -930,16 +930,33 @@
 ta =%3E %7B%0A
+%09let result = %7B%7D%0A
 %09if (arg
@@ -967,22 +967,16 @@
 th) %7B%0A%09%09
-const 
 result =
@@ -1117,49 +1117,21 @@
 %7D)%0A%09
-%09console.log(result)%0A%09%09return%0A%09%7D%0A%0A%09const 
+%7D%0A%0A%09else %7B%0A%09%09
 resu
@@ -1175,16 +1175,19 @@
 reaData)
+%0A%09%7D
 %0A%0A%09if (a
@@ -1450,60 +1450,71 @@
 log(
-'not implemented')%0A%09%09// console.log(summarize(result
+summarize(result, areaData.name, %5BareaData.type, areaData.name%5D
 ))%0A%09

build a 'summarize' implementation for evaluate