'use strict';

const config = require('config');
const os = require('os');
const { intervalToDuration } = require('date-fns');

const start = Date.now();

function getDurationString(duration) {
	return Object.entries(duration)
		.filter(([, value]) => !!value)
		.map(([key, value]) => `${value} ${key}`)
		.join(', ');
}

function onCommand(args, context) {
	const duration = intervalToDuration({ start, end: Date.now() });
	const durationString = getDurationString(duration);

	if (['sys', 'system'].includes(context.subcommand || args[0]) && config.operators.includes(context.user.username)) {
		const osDuration = intervalToDuration({ start: 0, end: os.uptime() * 1000 });
		const osDurationString = getDurationString(osDuration);

		context.sendMessage(`I've been awake for ${durationString}. My host has been running for ${osDurationString}.`, context.room.id, null, context.message.user?.recipient);

		return;
	}

	context.sendMessage(`I've been awake for ${durationString}.`, context.room.id, null, context.message.user?.recipient);
}

module.exports = { onCommand };
