import api from "../lib/axios";
import {messagesError} from '../helpers/validations';

export const login = async(body) => {
    try {
        const endpoint = "/v1/account/authorize";
        const {data} = await api.post(endpoint, body);
        console.log(data);
        console.log(data.accessToken);
        if(data.accessToken !== null){
            if(data.user.job_role !== 'driver'){
                //TODO: Guardar token y datos del usuario
                return {
                    msg: 'success',
                    data
                };
            } else {
              //Remover datos de sesion
              return 'Rol no autorizado';
            }
        } else {
          return {
              msg: "email y/o password incorrectos",
              data: null
          };
        }
    } catch (error) {
        const errStr = error.response.data.error ?? 'Algo salio mal, intente más tarde';
        return {
            msg: messagesError(errStr),
            data: null
        };
    }
}

export const renewToken = async() => {
    const session = localStorage.getItem('session');
    try {
        const endpoint = `/v1/account/authorize/${session}`;
        const {data} = await api.get(endpoint);
        console.log(data);
        if(data.accessToken !== null){
            return {
                msg: 'success',
                data
            };
        } else {
          return {
              msg: "Sesion expiro",
              data: null
          };
        }
    } catch (error) {
        const errStr = error.response.data.error ?? 'Algo salio mal, intente más tarde';
        return {
            msg: 'Sesion expiro',
            data: null
        };
    }
}


export const regiter = async(body) => {
    try {
        const endpoint = "/v1/account/signup";
        const {data} = await api.post(endpoint, body);
        return {
            msg: 'success',
            data
        };
    } catch (error) {
        return {
            msg: error.response.data.error ?? 'Algo salio mal, intente más tarde',
            data: null
        };
    }
}

export const regiterConfirm = async(body) => {
    try {
        const endpoint = "/v1/account/signup";
        const {data} = await api.patch(endpoint, body);
        return {
            msg: 'success',
            data
        };
    } catch (error) {
        let msg = 'Algo salio mal, intente más tarde';
        if(error.response.data.error) {
            if(error.response.data.error === 'Wrong OTP'){
                msg = 'Codigo ingresado incorrecto';
            } else {
                msg = error.response.data.error;
            }
        } 
        return {
            msg,
            data: null
        };
    }
}

export const recoveryPassword = async(body) => {
    try {
        const endpoint = "/v1/account/recover";
        const {data} = await api.post(endpoint, body);
        return {
            msg: 'success',
            data
        };
    } catch (error) {
        const errStr = error.response.data.error ?? 'Algo salio mal, intente más tarde';
        return {
            msg: messagesError(errStr),
            data: null
        };
    }
}

export const recoveryPasswordConfirm = async(body) => {
    try {
        const endpoint = "/v1/account/recover";
        const {data} = await api.patch(endpoint, body);
        return {
            msg: 'success',
            data
        };
    } catch (error) {
        let msg = 'Algo salio mal, intente más tarde';
        if(error.response.data.error) {
            if(error.response.data.error === 'Wrong OTP'){
                msg = 'Codigo ingresado incorrecto';
            } else {
                msg = error.response.data.error;
            }
        } 
        return {
            msg,
            data: null
        };
    }
}