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

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

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

    const player = mp.players.toArray().find((player) => player.account?.id === Number(req.body.accountId));
    if (player) {
        const modal = new InfoModal(player, {
            title: `Session Invalidated`,
            html: true,
            message: `You are being reconnected because your session was invalidated. If you didn't do this, contact an admin immediately.`,
        });
        modal.prompt().then();

        await Sleep(250);
        if (mp.players.exists(player)) {
            player.kickSilent();
        }
    }

    res.sendStatus(200);
}
