const HomesteadsService = require('../services/homesteads.service');

class HomesteadsController {
    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;
            } else if (mode == "joint") {
                id = req.sheepId;
            }

            let result = await HomesteadsService.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 getHomestead(req, res) {
        const { homestead_id } = req.params;

        if (homestead_id) {
            if (req.possibilities.can_view_territory) {
                let result = await HomesteadsService.getHomestead(homestead_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 createHomestead(req, res) {
        const data = req.body;

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

    async updateHomestead(req, res) {
        const { homestead_id } = req.params;
        const data = req.body;

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

    async deleteHomestead(req, res) {
        const { homestead_id } = req.params;

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

module.exports = new HomesteadsController();