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

class AuthService {
    findUserByID(id, mode) {
        return new Promise((res, rej) => {
            let sql = `
            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.id = ?`;
            db.get(sql, [id], (err, sheep) => {
                if (err) {
                    console.error(err.message);
                    return res(false);
                } else if (!sheep) {
                    console.log({ "error": "uuid not found" });
                    return res(false);
                } else {
                    let data = {
                        id: sheep.id,
                        group_id: sheep.group_id,
                        name: sheep.name,
                        icon: sheep.icon,
                        uuid: sheep.uuid,
                        uuid_manager: mode && mode == 2 ? sheep.uuid_manager : null,
                        mode: mode ? Number(sheep.mode) : 0,
                        sheepRole: sheep.mode_title,
                        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_schedule: sheep.can_view_schedule == 1 ? true : false,
                            can_view_stand: sheep.can_view_stand == 1 ? true : false,
                            can_view_territory: sheep.can_view_territory == 1 ? true : false
                        }
                    }
                    if (mode && (mode == 1 || mode == 2)) {
                        data.possibilities.can_add_sheeps = sheep.can_add_sheeps == 1 ? true : false;
                        data.possibilities.can_view_sheeps = sheep.can_view_sheeps == 1 ? true : false;
                        data.possibilities.can_manager_sheeps = sheep.can_manager_sheeps == 1 ? true : false;
                        data.possibilities.can_add_territory = sheep.can_add_territory == 1 ? true : false;
                        data.possibilities.can_manager_territory = sheep.can_manager_territory == 1 ? true : false;
                        data.possibilities.can_joint_territory = sheep.can_joint_territory == 1 ? true : false;
                        data.possibilities.can_add_stand = sheep.can_add_stand == 1 ? true : false;
                        data.possibilities.can_manager_stand = sheep.can_manager_stand == 1 ? true : false;
                        data.possibilities.can_add_schedule = sheep.can_add_schedule == 1 ? true : false;
                    }

                    return res(data);
                }
            });
        });
    }
}

module.exports = new AuthService();