module.exports = (lib) => {
	lib.trigger.add({
		name: 'f0ckrand',
		call: /^gib f0ck/i,
		level: 0,
		active: 1,
		func: (e) => {
			let args = e.message.trim().split(" ").slice(2);
			let params = {
				inc: [],
				exc: []
			};

			for(let i = 0; i < args.length; i++) {
				let name = args[0];
				if(name.charAt(0) === "!")
					params.exc.push(name.slice(1));
				else
					params.inc.push(name);
			}
			
			let vars = params.inc.concat(params.inc.length == 0 ? params.exc : []);
			params.inc = new Array(params.inc.length).fill('username LIKE ?');
			params.exc = new Array(params.inc.length == 0 ? params.exc.length : 0).fill('username NOT LIKE ?');
			let where = params.inc.concat(params.exc).join(' || ');
			let query = `SELECT id, username, mime, size FROM f0ck.items ${where.length > 0 ? `WHERE ${where}` : ''} ORDER BY rand() LIMIT 1`;

			lib.sql.query(query, vars, (err, rows) => {
				e.reply(!err && rows.length > 0 ? `f0ckrnd: ${lib.cfg.main.url}/${rows[0].id} by: ${rows[0].username} (${rows[0].mime}, ~${lib.formatSize(rows[0].size)})` : 'nothing found, f0cker');
			});
		},
		desc: 'Random-f0ck'
	});
};