const EntrancesService = require('../services/entrances.service');

class EntrancesController {
    async getEntrances(req, res) {
        const { house_id } = req.params;

        if (house_id) {
            if (req.possibilities.can_view_territory) {
                let result = await EntrancesService.getEntrances(house_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: 'House not found.' });
        }
    }

    async createEntrance(req, res) {
        const { house_id } = req.params;
        const data = req.body;

        if (house_id) {
            if (req.possibilities.can_add_territory) {
                let result = await EntrancesService.createEntrance(
                    house_id,
                    data
                );

                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable create entrance.',
                    });
                }
            } else {
                return res
                    .status(403)
                    .send({ message: 'The user does not have enough rights.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'House not found.' });
        }
    }

    async updateEntrance(req, res) {
        const data = req.body;

        if (data) {
            if (req.possibilities.can_add_territory) {
                let result = await EntrancesService.updateEntrance(data);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable update entrance.',
                    });
                }
            } else {
                return res
                    .status(403)
                    .send({ message: 'The user does not have enough rights.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'Data not found.' });
        }
    }

    async deleteEntrance(req, res) {
        const data = req.body;

        if (data) {
            if (req.possibilities.can_add_territory) {
                let result = await EntrancesService.deleteEntrance(data);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable delete entrance.',
                    });
                }
            } else {
                return res
                    .status(403)
                    .send({ message: 'The user does not have enough rights.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'Data not found.' });
        }
    }
}

module.exports = new EntrancesController();