const StandService = require('../services/stand.service');

class StandController {
    async getStand(req, res) {
        const { stand_id } = req.params;

        if (stand_id) {
            if (req.possibilities.can_view_stand) {
                const result = await StandService.getStand(stand_id);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(404).send({
                        message: 'Stand not found.'
                    });
                }
            } else {
                return res
                    .status(403)
                    .send({ message: 'The sheep does not have enough rights.' });
            }
        } else {
            return res
                .status(401)
                .send({ message: 'Stand not found.' });
        }
    }

    async getList(req, res) {
        if (req.possibilities.can_view_stand) {
            const result = await StandService.getList();

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

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

        if (data) {
            if (req.possibilities.can_add_stand) {
                let result = await StandService.createStand(data);

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

    async updateStand(req, res) {
        const { stand_id } = req.params;
        const data = req.body;

        if (stand_id && data) {
            if (req.possibilities.can_add_stand) {
                let result = await StandService.updateStand(stand_id, data);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable update stand.',
                    });
                }
            } else {
                return res
                    .status(403)
                    .send({ message: 'The user does not have enough rights.' });
            }
        } else {
            return res
                .status(401)
                .send({ message: 'stand_id or data not found.' });
        }
    }

    async deleteStand(req, res) {
        const { stand_id } = req.params;

        if (stand_id) {
            if (req.possibilities.can_add_stand) {
                let result = await StandService.deleteStand(stand_id);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable delete stend.',
                    });
                }
            } else {
                return res
                    .status(403)
                    .send({ message: 'The user does not have enough rights.' });
            }
        } else {
            return res
                .status(401)
                .send({ message: 'stand_id not found.' });
        }
    }

    async createSchedule(req, res) {
        const { stand_id } = req.params;

        if (stand_id) {
            if (req.possibilities.can_add_stand) {
                let result = await StandService.createSchedule(stand_id);

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

    async getScheduleList(req, res) {
        const { stand_id } = req.params;

        if (stand_id) {
            if (req.possibilities.can_view_stand) {
                const result = await StandService.getScheduleList(stand_id);

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

    async getScheduleHistory(req, res) {
        const { stand_schedule_id } = req.params;

        return res.status(200).send({ stand_schedule_id });
    }
}

module.exports = new StandController();