"use strict";

import path from 'path';

import CliParser from 'applause-cli';

import train from './subcommands/train/train.mjs';
import predict from './subcommands/predict/predict.mjs';

const __dirname = import.meta.url.slice(7, import.meta.url.lastIndexOf("/"));

export default async function () {
	let cli = new CliParser(path.resolve(__dirname, "../package.json"));
	cli.subcommand("train", "Trains a new AI")
		.argument("input", "The input directory containing the training data", null, "string")
		.argument("output", "Path to the output directory to save the trained AI to")
		.argument("image-size", "The width+height of input images (images are assumed to be square; default: 256)", 256, "integer")
		.argument("cross-entropy", "Use categorical cross-entropy loss instead of mean-squared error (best if each image has only a single label)", false, "boolean")
		.argument("activation", "Set the activation function for the CNN layer(s) (default: relu)", "relu", "string");
	cli.subcommand("predict", "Predicts the genres of the specified image")
		.argument("input", "Path to the input image")
		.argument("ai-model", "Path to the saved AI model to load");
	
	
	let settings = cli.parse(process.argv.slice(2));
	
	if(cli.current_subcommand == null) {
		cli.write_help_exit();
		return;
	}
	
	switch(cli.current_subcommand) {
		case "train":
			await train(settings);
			break;
		case "predict":
			await predict(settings);
			break;
		default:
			console.error(`Error: Unknown subcommand  '${cli.current_subcommand}' (try --help for usage information)`);
			process.exit(1);
			break;
	}
}
