import express from 'express';
import { ChatSystem } from '../../../../features/chat';
import { getRepository } from 'typeorm';
import { CharacterEntity } from '../../../../entities/character.entity';

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

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

    const player = mp.players.toArray().find((p) => p.character?.id === Number(req.params.characterId));
    if (!player?.character) {
        res.send('no');
        return;
    }

    player.character.max_companies = (await getRepository(CharacterEntity).findOne(player.id))!.max_companies;
    ChatSystem.Success(player, `Your company slots have been updated.`);
    res.send('yes');
}
