import { getUserRecord, updateUserRecord } from "db/dbUser";
import { emptyInventory } from "items";
import { Command, sendMessage } from "lib/commandUtils";
import { addInvuln } from "lib/invuln";
import parseCommandArgs from "lib/parseCommandArgs";
import User from "user";

export default new Command({
	name: "addinvuln",
	aliases: ["addinvuln"],
	usertype: "moderator",
	disableable: false,
	execution: async (msg) => {
		const args = parseCommandArgs(msg.messageText);
		if (!args[0]) {
			await sendMessage("Please specify a target", msg.messageId);
			return;
		}
		const target = await User.initUsername(args[0].toLowerCase());
		if (!target) {
			await sendMessage(`Chatter ${args[0]} doesn't exist`, msg.messageId);
			return;
		}
		const data = await addInvuln(target.id);
		if (data === "OK") {
			const userRecord = await getUserRecord(target);
			userRecord.inventory = emptyInventory;
			userRecord.balance = 0;
			await updateUserRecord(target, userRecord);
			await sendMessage(
				`${target.displayName} is now an invuln. Their inventory and wallet have been wiped`,
				msg.messageId,
			);
		} else
			await sendMessage(
				`${target.displayName} is already an invuln`,
				msg.messageId,
			);
	},
});
