const SheepsService = require('../services/sheeps.service');

class SheepsController {
    async getSheep(req, res) {
        const { id } = req.query;

        if (id) {
            if (req.possibilities.can_view_sheeps) {
                const result = await SheepsService.getSheep(id, req.mode);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(404).send({
                        message: 'Sheep not found.'
                    });
                }
            } else {
                return res
                    .status(403)
                    .send({ message: 'The sheep does not have enough rights.' });
            }
        } else {
            return res
                .status(401)
                .send({ message: 'Sheeps not found.' });
        }
    }

    async getList(req, res) {
        if (req.possibilities.can_view_sheeps) {
            const result = await SheepsService.getList(req.mode);

            if (result) {
                return res.status(200).send(result);
            } else {
                return res
                    .status(404)
                    .send({ message: 'User not found.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'The sheep does not have enough rights.' });
        }
    }

    async getListStand(req, res) {
        if (req.possibilities.can_view_stand) {
            const result = await SheepsService.getListStand(req.mode);

            if (result) {
                return res.status(200).send(result);
            } else {
                return res
                    .status(404)
                    .send({ message: 'User not found.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'The sheep does not have enough rights.' });
        }
    }

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

        if (req.possibilities.can_add_sheeps) {
            let result = await SheepsService.createSheep(data);

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

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

        if (req.mode == 2) {
            let result = await SheepsService.updateSheep(data, 2);

            if (result) {
                return res.status(200).send(result);
            } else {
                return res.status(500).send({
                    message: 'Unable update sheep.',
                });
            }
        } if (req.possibilities.can_manager_sheeps) {           
            if (Number(data.mode) == 2) {
                return res.status(403).send({ message: 'The sheep does not have enough rights.' });
            } else {
                let result = await SheepsService.updateSheep(data, 1);

                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable update sheep.',
                    });
                }
            }
        } else {
            return res
                .status(403)
                .send({ message: 'The sheep does not have enough rights.' });
        }
    }

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

        if (req.mode == 2) {
            let result = await SheepsService.deleteSheep(data);
            if (result) {
                return res.status(200).send(result);
            } else {
                return res.status(500).send({
                    message: 'Unable delete sheep.',
                });
            }
        } else {
            return res
                .status(403)
                .send({ message: 'Sheep not foundThe sheep does not have enough rights.' });
        }
    }
}

module.exports = new SheepsController();