import * as constants from './constants.js'

//Returns matters from database based on the currently logged in user ID
export function  home_itemsfeed_get(req, result) {
	const selectColumns = constants.requiredColumns + ',' +
	'm.mttitle AS "Matter Title",' +
	'm.mtname AS "Matter Name",' +
	'm.mtcontract AS "Contract Num",' +
	'u.udisplay AS "Assignee",' +
	'm.mtsubtype AS "Matter Type",' +
	'm.mtdeadline AS "Deadline"'

	const clauses = {
		joinUser:'JOIN (SELECT ukey, udisplay FROM users) AS u ON m.mtuser = u.ukey',
		whereUser:'u.ukey = $1'
	}

	const sqlQuery_text = 'SELECT ' + selectColumns + ' FROM matter AS m ' + clauses.joinUser +
	 ` WHERE (${clauses.whereUser})`;

	const sqlQuery_values = [req.query.q_uid];

	const sqlQuery = {
		text: sqlQuery_text,
		values: sqlQuery_values
	}

	req.app.locals.pool.query(sqlQuery)
		.then((queryResults) => {
			result.status(200).json({
				results_matters:queryResults.rows
			});
		})
		.catch( (err) => {
			console.log('SQL Error: ' + err.stack);
		})
};

export function  home_itemsfeed_recent_get(req, result) {
	result.send('NOT IMPLEMENTED');
};

export function  home_itemsfeed_relevant_get(req, result) {
	result.send('NOT IMPLEMENTED');
};

export function  home_itemsfeed_priority_get(req, result) {
	result.send('NOT IMPLEMENTED');
};
