const axios = require('axios');
let auth_status = 0;

async function check_auth_status() {
    return await new Promise(resolve => {
        const check = setInterval(() => {
            console.log("Checking auth status...");
    
            if(auth_status == 1) {
                console.log("Bot has been authenticated");
                resolve(true);
                clearInterval(check);
                return true;
            }
        }, 2000);
    });
};

function verify_auth_status() {
    auth_status = 1; 
    return true; 
}

async function get_access_token(host, session_id) {
    const response = await axios.post(`${host}/api/miauth/${session_id}/check`, null, {
        headers: {
            'Content-Type': 'application/json'
        }
    })

    if(response.data.ok == true) {
        console.log("Successfully retrieved access token");
        return response.data.token;
    } else {
        return false; 
    }
};

module.exports.check_auth_status = check_auth_status;
module.exports.verify_auth_status = verify_auth_status;
module.exports.get_access_token = get_access_token;