const PushService = require('../services/push.service');

class PushController {
    async getKey(req, res) {
        let result = await PushService.getKey();

        if (result) {
            return res.status(200).send(result);
        } else {
            return res.status(404).send({
                message: 'Key not found.'
            });
        }
    }

    async createSubscribe(req, res) {
        const data = req.body;
        if (data) {
            if (req.sheepId) {
                let result = await PushService.createSubscribe(req.sheepId, data);
                
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable create subscribe.',
                    });
                }
            } else {
                return res
                    .status(401)
                    .send({ message: 'Sheeps not found.' });
            }
        } else {
            return res
                .status(401)
                .send({ message: 'Data not found.' });
        }
    }

    async deleteSubscribe(req, res) {
        const data = req.body;
        if (data) {
            if (req.sheepId) {
                let result = await PushService.deleteSubscribe(data);
                
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable delete subscribe.',
                    });
                }
            } else {
                return res
                    .status(401)
                    .send({ message: 'Sheeps not found.' });
            }
        } else {
            return res
                .status(401)
                .send({ message: 'Data not found.' });
        }
    }
}

module.exports = new PushController();