const HistoryHomesteadService = require('../services/history.homestead.service');

class HistoryHomesteadController {
    async getHistoryHomestead(req, res) {
        const { homestead_id } = req.params;

        if (homestead_id) {
            if (req.possibilities.can_manager_territory) {
                let result = await HistoryHomesteadService.getHistoryHomestead(homestead_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(404)
                .send({ message: 'Users not found.' });
        }
    }

    async createHistoryHomestead(req, res) {
        const { homestead_id } = req.params;
        const data = req.body;

        if (homestead_id) {
            if (req.possibilities.can_manager_territory) {
                let result = await HistoryHomesteadService.createHistoryHomestead(
                    homestead_id,
                    data
                );

                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable create history homestead.',
                    });
                }
            } else {
                return res
                    .status(404)
                    .send({ message: 'User not found.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'Users not found.' });
        }
    }

    async updateHistoryHomestead(req, res) {
        const { uuid } = req.query;
        const { homestead_id } = req.params;

        if (homestead_id) {
            if (req.possibilities.can_manager_territory) {
                let result = await HistoryHomesteadService.updateHistoryHomestead(homestead_id);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable update history homestead.',
                    });
                }
            } else {
                return res
                    .status(404)
                    .send({ message: 'User not found.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'Users not found.' });
        }
    }

    async deleteHistoryHomestead(req, res) {
        const { homestead_id } = req.params;

        if (homestead_id) {
            if (req.possibilities.can_manager_territory) {
                let result = await HistoryHomesteadService.deleteHistoryHomestead(homestead_id);
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable delete history homestead.',
                    });
                }
            } else {
                return res
                    .status(404)
                    .send({ message: 'User not found.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'Users not found.' });
        }
    }
}

module.exports = new HistoryHomesteadController();