const HomesteadJointService = require('../services/homestead.joint.service');

class HomesteadJointController {
    async getList(req, res) {
        const { homestead_id } = req.params;

        if (homestead_id) {
            if (req.possibilities.can_joint_territory) {
                let result = await HomesteadJointService.getList(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 createJoint(req, res) {
        const { homestead_id } = req.params;
        const data = req.body;

        if (homestead_id) {
            if (req.possibilities.can_joint_territory) {
                let result = await HomesteadJointService.createJoint(
                    homestead_id,
                    data
                );

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

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

        if (homestead_id) {
            if (req.possibilities.can_joint_territory) {
                let result = await HomesteadJointService.deleteJoint(
                    homestead_id,
                    data
                );
                if (result) {
                    return res.status(200).send(result);
                } else {
                    return res.status(500).send({
                        message: 'Unable delete joint homestead.',
                    });
                }
            } else {
                return res
                    .status(404)
                    .send({ message: 'User not found.' });
            }
        } else {
            return res
                .status(404)
                .send({ message: 'Users not found.' });
        }
    }
}

module.exports = new HomesteadJointController();