import { Cheer, handleNoTarget } from "cheers";
import { createCheerEventRecord } from "db/dbCheerEvents";
import { createTimeoutRecord } from "db/dbTimeouts";
import { getUserRecord } from "db/dbUser";
import { getTNTTargets } from "items/tnt";
import { sendMessage } from "lib/commandUtils";
import { redis } from "lib/redis";
import { timeout } from "lib/timeout";
import User from "user";
import { playAlert } from "web/alerts/serverFunctions";

const ITEMNAME = "tnt";

export default new Cheer({
	name: "tnt",
	amount: 1000,
	isItem: true,
	async execute(msg, user) {
		const vulntargets = await redis
			.keys("user:*:vulnerable")
			.then((a) => a.map((b) => b.slice(5, -11)));
		if (vulntargets.length === 0) {
			await sendMessage("No vulnerable chatters to blow up", msg.messageId);
			await handleNoTarget(msg, user, ITEMNAME);
			return;
		}
		const targets = getTNTTargets(vulntargets);

		await Promise.all(
			targets.map(async (targetid) => {
				const target = await User.initUserId(targetid);
				await getUserRecord(target!); // make sure the user record exist in the database
				await Promise.all([
					timeout(target!, `You got hit by ${user.displayName}'s TNT!`, 60),
					redis.del(`user:${targetid}:vulnerable`),
					sendMessage(
						`wybuh ${target?.displayName} got hit by ${user.displayName}'s TNT wybuh`,
					),
					createTimeoutRecord(user, target!, ITEMNAME),
				]);
			}),
		);

		await Promise.all([
			createCheerEventRecord(user, ITEMNAME),
			playAlert({
				name: "tntExplosion",
				user: user.displayName,
				targets,
			}),
		]);

		await sendMessage(
			`RIPBOZO ${user.displayName} exploded ${targets.length} chatter${targets.length === 1 ? "" : "s"} with their TNT RIPBOZO`,
		);
	},
});
