const { HttpsProxyAgent } = require('https-proxy-agent');
const axios = require('axios');

const CONFIG = {
    ENDPOINT_URL_APPS: 'https://rpa8dev.okan.tools/users/apps/byUserAdOrDocument',
    ENDPOINT_URL_APP: 'https://rpa8dev.okan.tools/app',
    ENDPOINT_OKAN_TOKEN: 'https://io.okan.tools/api/auth/electron',
    TOKEN_OKAN_USER: 'HrZTvmBNyQaM6jPI7sHo5ywN35ht/cplIBFeE+4Ufx8=',
    PROXY: 'http://proxyop.bop.local:8080',
    ENDPOINT_GET_PROFILE: 'https://io.okan.tools/api/auth/users/electron',
    ENDPOINT_GET_APPS: 'https://adm.okan.tools/wp-json/okanapiwp/v1/aplicativos/'
};

async function test() {
// try {
//     const agent = new HttpsProxyAgent(CONFIG.PROXY);
//     const responseOkan = await axios.post(CONFIG.ENDPOINT_OKAN_TOKEN,
//          { user: '45957768' }, {
//         headers: { 'Authorization': CONFIG.TOKEN_OKAN_USER },
//         httpsAgent: agent
//     });
//     if (responseOkan.data.cod == '0') {
//         console.log('>> Token de Okan obtenido.');
//         console.log(responseOkan.data.data);
//         return responseOkan.data.data;
//     } else {
//         console.log('>> Error al obtener el token de Okan.');
//         return null;
//     }
// } catch (error) {
//     console.log('>> Error en la petición del token de Okan:', error);
// }

// const token_okan = 'Bearer ZXlKaGJHY2lPaUpJVXpJMU5pSXNJblI1Y0NJNklrcFhWQ0o5LmV5SmxiV0ZwYkVOdmNuQnZjbUYwYVhabElqb2lZbkpoYW1GdUxtRnNaR0Z1WVVCMFlYSnpMbVJsZGlJc0ltVnRZV2xzVUdWeWMyOXVZV3dpT2lKa2NtRmphM2gxYzBCbmJXRnBiQzVqYjIwaUxDSmtiMk4xYldWdWRDSTZJakV3TWpJNU5qazFPVFlpTENKdWFXTnJibUZ0WlNJNklqUTFPVFUzTnpZNElpd2lablZzYkU1aGJXVWlPaUppY21GcVlXNGdhbTloYm5rZ1lXeGtZVzVoSUNCeVpYbGxjeUlzSW1WNGNHbHlaVVJoZEdVaU9pSXlNREkxTFRBMExUQXpJREUxT2pNek9qQTRJbjA9Lmg0UERaMEFmcDJsYXk1YzNEektHYldSRkpDUUNIemhTN2phRXJrYnlIb2M9';

//     try {
//         const agent = new HttpsProxyAgent(CONFIG.PROXY);
        
//         const responseGetProfile = await axios.get(CONFIG.ENDPOINT_GET_PROFILE, { headers: { 'Authorization': token_okan },
//             httpsAgent: agent });
//         if (responseGetProfile.data.cod == '0') {
//             documento = responseGetProfile.data.data.document_number;
//             profile = responseGetProfile.data.data.profile.id;
//             console.log('>> Perfil: ' + profile);
//             return profile;
//         }
//     } catch (e) {
//         console.log('>> No se pudo obtener el perfil: ' + e);
//     }



try {
    const agent_three = new HttpsProxyAgent(CONFIG.PROXY);

    let documento = '1022969596';
    let profile = '8475';
    
    const urlAplicativos = CONFIG.ENDPOINT_GET_APPS + `${documento}/${profile}`;
    const responseAplicativos = await axios.get(urlAplicativos, {
        headers: { 
            "Accept": "*/*", 
            "Content-Type": "application/json"
        },
        httpsAgent: agent_three,  // Changed from httpAgent to httpsAgent
        proxy: false  // Disable default proxy to ensure our custom agent is used
    });

    var apps_okan = [];
    if (responseAplicativos.data['cod'] == '0') {

        const new_apps = responseAplicativos.data['data']
            .filter(element => element['nombre_app'].startsWith('RPA'))
            .sort((a, b) => a.nombre_app.localeCompare(b.nombre_app));

        console.log('>> Aplicativos Okan: ' + new_apps.length);

    }
} catch (e) {
    console.log('>> Error al obtener aplicaciones:', e.message);  // Enhanced error logging
}
}
test();