const db = require("../config/db");

const authenticate = (req, res, next) => {
    const uuid = req.headers["authorization"];
    if (!uuid) return res.status(401).json({ error: "Unauthorized" });

    db.get(`
        SELECT 
            sheeps.*, 
            possibilities.can_add_sheeps AS can_add_sheeps,
            possibilities.can_view_sheeps AS can_view_sheeps,
            possibilities.can_manager_sheeps AS can_manager_sheeps,
            possibilities.can_add_territory AS can_add_territory,
            possibilities.can_view_territory AS can_view_territory,
            possibilities.can_manager_territory AS can_manager_territory,
            possibilities.can_joint_territory AS can_joint_territory,
            possibilities.can_add_stand AS can_add_stand,
            possibilities.can_view_stand AS can_view_stand,
            possibilities.can_manager_stand AS can_manager_stand,
            possibilities.can_add_schedule AS can_add_schedule,
            possibilities.can_view_schedule AS can_view_schedule
        FROM 
            sheeps
        LEFT JOIN 
            possibilities ON possibilities.sheep_id = sheeps.id
        WHERE 
            sheeps.uuid_manager = ?`,
        [uuid],
        (err, moderator) => {
            if (moderator) {
                req.sheepId = moderator.id;
                req.sheepName = moderator.name;
                req.group_id = moderator.group_id;
                req.mode = Number(moderator.mode);
                req.possibilities = {
                    can_add_sheeps: moderator.can_add_sheeps == 1 ? true : false,
                    can_view_sheeps: moderator.can_view_sheeps == 1 ? true : false,
                    can_manager_sheeps: moderator.can_manager_sheeps == 1 ? true : false,
                    can_add_territory: moderator.can_add_territory == 1 ? true : false,
                    can_view_territory: moderator.can_view_territory == 1 ? true : false,
                    can_manager_territory: moderator.can_manager_territory == 1 ? true : false,
                    can_joint_territory: moderator.can_joint_territory == 1 ? true : false,
                    can_add_stand: moderator.can_add_stand == 1 ? true : false,
                    can_view_stand: moderator.can_view_stand == 1 ? true : false,
                    can_manager_stand: moderator.can_manager_stand == 1 ? true : false,
                    can_add_schedule: moderator.can_add_schedule == 1 ? true : false,
                    can_view_schedule: moderator.can_view_schedule == 1 ? true : false
                }

                return next();
            }

            db.get(`
                SELECT 
                    sheeps.*, 
                    possibilities.can_add_sheeps AS can_add_sheeps,
                    possibilities.can_view_sheeps AS can_view_sheeps,
                    possibilities.can_manager_sheeps AS can_manager_sheeps,
                    possibilities.can_add_territory AS can_add_territory,
                    possibilities.can_view_territory AS can_view_territory,
                    possibilities.can_manager_territory AS can_manager_territory,
                    possibilities.can_add_stand AS can_add_stand,
                    possibilities.can_view_stand AS can_view_stand,
                    possibilities.can_manager_stand AS can_manager_stand,
                    possibilities.can_add_schedule AS can_add_schedule,
                    possibilities.can_view_schedule AS can_view_schedule
                FROM 
                    sheeps
                LEFT JOIN 
                    possibilities ON possibilities.sheep_id = sheeps.id
                WHERE 
                    sheeps.uuid = ?`,
                [uuid],
                (err, sheep) => {
                    if (sheep) {
                        req.sheepId = sheep.id;
                        req.sheepName = sheep.name;
                        req.group_id = sheep.group_id;
                        req.uuid_manager = null;
                        req.mode = 0;
                        req.possibilities = {
                            can_add_sheeps: false,
                            can_view_sheeps: false,
                            can_manager_sheeps: false,
                            can_add_territory: false,
                            can_manager_territory: false,
                            can_joint_territory: false,
                            can_add_stand: false,
                            can_manager_stand: false,
                            can_add_schedule: false,
                            can_view_territory: sheep.can_view_territory == 1 ? true : false,
                            can_view_stand: sheep.can_view_stand == 1 ? true : false,
                            can_view_schedule: sheep.can_view_schedule == 1 ? true : false
                        }

                        return next();
                    }

                    return res.status(401).json({ error: "UUID not found" });
                }
            );
        }
    );
};

module.exports = authenticate;