const fs = require('fs');
const path = require('path');
const log = require('loglevel');

class EmployeeServ {
    constructor () {
        this.axios = global.axiosInstances.axiosAfcInstance;
    }

    login = async (identifierType, credentials) => {
        try {
            const res = await this.axios.post('employee/login/'+identifierType, credentials);
            if (typeof res.data === 'object' && res.data.hasOwnProperty('errorMessage')) { return res.data; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getByUsername = async (identifierType, username) => {
        try {
            const res = await this.axios.get('employee/'+identifierType+'/'+username);
            if (typeof res.data === 'object' && (res.errors || res.data.hasOwnProperty('errorMessage'))) { return null; }
            if (typeof res === 'string' && res.includes('503 Service Unavailable')) { return 0; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    changePassword = async (employeeId, newPassword) => {
        try {
            const res = await this.axios.put('employee/'+employeeId+'/set-password', newPassword);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getUsersForType = async (deviceType) => {
        try {
            const res = await this.axios.get('employee/'+deviceType);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    pushEmployee = async (employeesList) => {
        try {
            const res = await this.axios.post('employee', employeesList);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

}

module.exports = EmployeeServ;
