import express from 'express';
import { InfoModal } from '../../../../features/modal/modals/info.modal';
import { SafeInsert } from '../../../../features/utility/SafeInsert';

export async function AccountBanned(req: express.Request, res: express.Response): Promise<void> {
    if (!req.body.accountId) {
        res.status(400).send('Missing parameter');
    }

    if (!Number(req.body.accountId)) {
        res.status(500).send('Unable to parse given parameter');
    }

    const player = mp.players.toArray().find((player) => player.account?.id === Number(req.body.accountId));
    if (player) {
        const modal = new InfoModal(player, {
            title: `Banned`,
            html: true,
            message: SafeInsert(`You've been banned from the server by {{ bannedBy }} for the following reason:<br><br><strong>{{ reason }}</strong>`, {
                bannedBy: req.body.by,
                reason: req.body.reason,
            }),
        });
        modal.prompt().then();

        setTimeout(() => {
            if (mp.players.exists(player)) {
                player.kick('Banned');
            }
        }, 1000);
    }

    res.sendStatus(200);
}
