const ApartmentsService = require('../services/apartments.service');

class ApartmentsController {
    async getApartments(req, res) {
        const { entrance_id } = req.params;

        if (entrance_id) {
            if (req.possibilities.can_view_territory) {
                let result = await ApartmentsService.getApartments(entrance_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: 'Entrance not found.' });
        }
    }

    async createApartments(req, res) {
        const { entrance_id } = req.params;
        const data = req.body;

        if (entrance_id && data) {
            if (req.possibilities.can_add_territory) {
                let result = await ApartmentsService.createApartments(
                    entrance_id,
                    data
                );

                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable create apartment.',
                    });
                }
            } 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 updateApartments(req, res) {
        const data = req.body;

        if (data) {
            if (req.possibilities.can_add_territory) {
                let result = await ApartmentsService.updateApartments(data);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable update history apartment.',
                    });
                }
            } 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 deleteApartments(req, res) {
        const data = req.body;

        if (data) {
            if (req.possibilities.can_add_territory) {
                let result = await ApartmentsService.deleteApartments(data);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable delete history apartment.',
                    });
                }
            } else {
                return res
                .status(403)
                .send({ message: 'The user does not have enough rights.' });
            }
        } else {
            return res
                .status(401)
                .send({ message: 'Data not found.' });
        }
    }
}

module.exports = new ApartmentsController();