"use strict";

const Promise = require("bluebird");
const asTable = require("as-table");

const createClient = require("../src/client");
const { select, collapseBy, hierarchical, compute, count, sum, where, anyOf, allOf, parameter, unsafeSQL, moreThan } = require("../src/operations");

// let query = select("sales", [
// 	// where({ size: moreThan(anyOf(parameter("sizes"))) }), // should work
// 	// where({ size: moreThan(anyOf([1, 2, 3])) }), // should work
// 	where({ size: moreThan(anyOf([1, allOf([2, 3]) ])) }), // should work
// 	// where({ size: anyOf(unsafeSQL("")) }),
// 	collapseBy([ "color", "size", hierarchical([ "country_id", "store_id" ]) ], [
// 		compute({
// 			total_sold: count(),
// 			total_revenue: sum("price")
// 		})
// 	])
// ]);

let query = select("sales", [
	where({ size: anyOf(parameter("sizes")) }),
	collapseBy(["size"], [
		compute({ total_revenue: sum("price") })
	])
]);

let client = createClient({
	socket: "/var/run/postgresql",
	database: "movietest"
});

return Promise.try(() => {
	let sizes = [ "S", "M", "L", "XL" ];

	return client.query(query, { sizes });
}).then((results) => {
	console.log(asTable(results));

	return client.destroy();
});

