const Utilities = require('../../Utilities')
const Exception = require('../../Exception')

module.exports = function verify(username, password) {
	const internals = arguments[0]
	username = arguments[1]
	password = arguments[2]
	const self = this
	return new Promise(function(resolve, reject) {
		Utilities.verifyParameter(
			'username',
			username,
			[String]
		)
		Utilities.verifyParameter(
			'password',
			password,
			[String]
		)
		var userProfile
		self.users.find({
			username: username
		})
			.then(function(foundUser) {
				if(!foundUser) {
					return resolve()
				}
				userProfile = foundUser
				return Utilities.matchSecureHash(
					userProfile.password,
					password
				)
			})
			.then(function(result) {
				if(!result) {
					return resolve(null)
				}
				return resolve({
					id: userProfile.id,
					username: userProfile.username,
					password: userProfile.password,
					creationDatetime: userProfile.creation_datetime,
					modificationDatetime: userProfile.modification_datetime
				})
			})
			.catch(function(error) {
				return reject(error)
			})
	})
}