const HousesService = require('../services/houses.service');

class HousesController {
    async getListEntrance(req, res) {
        if (req.possibilities.can_manager_territory) {

            let result = await HousesService.getListEntrance();
            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.' });
        }
    }

    async getList(req, res) {
        const { mode, sheep_id } = req.query;

        if (req.possibilities.can_view_territory) {
            let id = null;

            if (mode == "admin" && req.possibilities.can_manager_territory) {
                id = sheep_id;
            } else if (mode == "sheep") {
                id = req.sheepId;
            } else if (mode == "group") {
                id = req.group_id;
            }
            
            let result = await HousesService.getList(mode, 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.' });
        }
    }

    async getHouse(req, res) {
        const { house_id } = req.params;

        if (house_id) {
            if (req.possibilities.can_view_territory) {
                let result = await HousesService.getHouse(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: 'User not found.' });
        }
    }

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

        if (req.possibilities.can_add_territory) {
            let result = await HousesService.createHouse(data);
            if (result) {
                return res.status(200).send(result);
            } else {
                return res.status(500).send({
                    message: 'Unable create house.',
                });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'User not found.' });
        }
    }

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

        if (house_id) {
            if (req.possibilities.can_manager_territory) {
                let result = await HousesService.updateHouse(house_id, data);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable update house.',
                    });
                }
            } else {
                return res
                    .status(404)
                    .send({ message: 'User not found.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'Users not found.' });
        }
    }

    async deleteHouse(req, res) {
        const { house_id } = req.params;

        if (house_id) {
            if (req.possibilities.can_add_territory) {
                let result = await HousesService.deleteHouse(house_id);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable delete house.',
                    });
                }
            } else {
                return res
                    .status(404)
                    .send({ message: 'User not found.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'Users not found.' });
        }
    }
}

module.exports = new HousesController();