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

async function auth(uuid) {
    return new Promise((res, rej) => {
        db.get(`
            SELECT 
                sheeps.*,
                possibilities.can_add_sheeps,
                possibilities.can_view_sheeps,
                possibilities.can_add_territory,
                possibilities.can_view_territory,
                possibilities.can_manager_territory,
                possibilities.can_add_stand,
                possibilities.can_view_stand,
                possibilities.can_manager_stand,
                possibilities.can_add_schedule,
                possibilities.can_view_schedule
            FROM sheeps
            LEFT JOIN possibilities ON possibilities.sheep_id = sheeps.id
            WHERE sheeps.uuid_manager = ? OR sheeps.uuid = ?
        `, [uuid, uuid], (err, row) => {
            if (err) return rej(err);
            if (!row) return res(false);

            res({
                id: row.id,
                name: row.name,
                uuid: row.uuid,
                group_id: row.group_id,
                icon: row.icon,
                appointment: row.appointment,
                sheepRole: row.mode_title,
                possibilities: {
                    can_add_sheeps: !!row.can_add_sheeps,
                    can_view_sheeps: !!row.can_view_sheeps,
                    can_add_territory: !!row.can_add_territory,
                    can_view_territory: !!row.can_view_territory,
                    can_manager_territory: !!row.can_manager_territory,
                    can_add_stand: !!row.can_add_stand,
                    can_view_stand: !!row.can_view_stand,
                    can_manager_stand: !!row.can_manager_stand,
                    can_add_schedule: !!row.can_add_schedule,
                    can_view_schedule: !!row.can_view_schedule,
                }
            });
        });
    });
}

module.exports = { auth };