const BuildingsService = require('../services/buildings.service');

class BuildingsController {
    async getList(req, res) {
        const { id } = req.params;

        if (id) {
            if (req.possibilities.can_view_territory) {
                let result = await BuildingsService.getList(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(401)
                .send({ message: 'Buildings not found.' });
        }
    }

    async createBuildings(req, res) {
        const { id } = req.params;
        const data = req.body;

        if (id && data) {
            if (req.possibilities.can_add_territory) {
                let result = await BuildingsService.createBuildings(
                    id,
                    data
                );

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

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

        if (data) {
            if (req.possibilities.can_add_territory) {
                let result = await BuildingsService.updateBuildings(data);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable update building.',
                    });
                }
            } 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 deleteBuildings(req, res) {
        const { id } = req.params;

        if (id) {
            if (req.possibilities.can_add_territory) {
                let result = await BuildingsService.deleteBuildings(id);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable delete building.',
                    });
                }
            } else {
                return res
                .status(403)
                .send({ message: 'The user does not have enough rights.' });
            }
        } else {
            return res
                .status(401)
                .send({ message: 'building id not found.' });
        }
    }
}

module.exports = new BuildingsController();