const HistoryEntranceService = require('../services/history.entrance.service');

class HistoryEntranceController {
    async getHistoryEntrance(req, res) {
        const { entrance_id } = req.params;

        if (entrance_id) {
            if (req.possibilities.can_manager_territory) {
                let result = await HistoryEntranceService.getHistoryEntrance(entrance_id);
                if (result) {
                    return res
                        .status(200)
                        .send(result);
                } else {
                    return res
                        .status(500)
                        .send({ message: 'Internal server error.' });
                }
            } else {
                return res
                    .status(403)
                    .send({ message: 'The user does not have enough rights.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'Users not found.' });
        }
    }

    async createHistoryEntrance(req, res) {
        const { entrance_id } = req.params;
        const data = req.body;

        if (entrance_id) {
            if (req.possibilities.can_manager_territory) {
                let result = await HistoryEntranceService.createHistoryEntrance(
                    entrance_id,
                    data
                );

                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable create history entrance.',
                    });
                }
            } else {
                return res
                    .status(404)
                    .send({ message: 'User not found.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'Users not found.' });
        }
    }

    async updateHistoryEntrance(req, res) {
        const { entrance_id } = req.params;

        if (entrance_id) {
            if (req.possibilities.can_manager_territory) {
                let result = await HistoryEntranceService.updateHistoryEntrance(entrance_id);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable update history entrance.',
                    });
                }
            } else {
                return res
                    .status(404)
                    .send({ message: 'User not found.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'Users not found.' });
        }
    }

    async deleteHistoryEntrance(req, res) {
        const { entrance_id } = req.params;

        if (entrance_id) {
            if (req.possibilities.can_manager_territory) {
                let result = await HistoryEntranceService.deleteHistoryEntrance(entrance_id);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable delete history entrance.',
                    });
                }
            } else {
                return res
                    .status(404)
                    .send({ message: 'User not found.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'Users not found.' });
        }
    }
}

module.exports = new HistoryEntranceController();