import api from "../lib/axios";


export const getCompany = async() => {
    try {
        const endpoint = `/v1/companies/own`;
        const {data} = await api.get(endpoint);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const updateCompany = async(formData) => {
    try {
        const endpoint = `/v1/companies/own`;
        const {data} = await api.patch(endpoint, formData);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const getUsers = async(filter) => {
    try {
        const endpoint = `/v1/users/find?${filter}&$sort%5BcreatedAt%5D=-1`;
        const {data} = await api.get(endpoint);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const createUser = async(formData) => {
    try {
        const endpoint = `/v1/users/member`;
        const {data} = await api.post(endpoint, formData);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const updateUser = async(user_id, formData) => {
    try {
        const endpoint = `/v1/users/member/${user_id}`;
        const {data} = await api.patch(endpoint, formData);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const updateMyUserProfile = async(formData) => {
    try {
        const endpoint = `/v1/users/profile`;
        const {data} = await api.patch(endpoint, formData);
        return {
            msg: "success",
            data: data
        };
    } catch (error) {
        console.log(error);
        return {
            msg: error.response.data.error,
            data: null
        };
    }
}

export const deleteUser = async(user_id) => {
    try {
        const endpoint = `/v1/users/member/${user_id}`;
        const {data} = await api.delete(endpoint);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const getBudgets = async(filter) => {
    try {
        const endpoint = `v1/budgets/find${filter}&sort[createdAt]=-1`;
        const {data} = await api.get(endpoint);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}


export const updateBudget = async(id, formData) => {
    try {
        const endpoint = `v1/budgets/${id}`;
        const {data} = await api.patch(endpoint, formData);
        console.log(data);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const createBudget = async(formData) => {
    try {
        const endpoint = `v1/budgets/new`;
        const {data} = await api.post(endpoint, formData);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const deleteBudget = async(id) => {
    try {
        const endpoint = `v1/budgets/${id}`;
        const {data} = await api.delete(endpoint);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const getLocations = async(filter) => {
    try {
        const endpoint = `/v1/branches/find${filter}&$sort%5BcreatedAt%5D=-1`;
        console.log(endpoint);
        const {data} = await api.get(endpoint);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const createLocation = async(formData) => {
    try {
        const endpoint = `/v1/branches/new`;
        const {data} = await api.post(endpoint, formData);
        return {data};
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const updateLocation = async(id, formData) => {
    try {
        const endpoint = `/v1/branches/${id}`;
        const {data} = await api.patch(endpoint, formData);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const deleteLocation = async(id) => {
    try {
        const endpoint = `/v1/branches/${id}`;
        const {data} = await api.delete(endpoint);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const getCalendar = async(startDate, endDate) => {
    try {
        const endpoint = `/v1/loads/calendar?$sort[createdAt]=-1&date[lte]=${endDate}&date[gte]=${startDate}&elements=1000`;
        console.log(endpoint);
        const {data} = await api.get(endpoint);
        return {
            msg: "success",
            data: data
        };
    } catch (error) {
        console.log(error);
        return {
            msg: "Algo salió mal, intente nás tarde",
            data: null
        };
    }
}


// export const editCompany = async(companyId, formData) => {
//     try {
//         const endpoint = `/companies/${companyId}`;
//         const {data} = await api.patch(endpoint, formData);
//         return data;
//     } catch (error) {
//         console.log(error);
//         return null;
//     }
// }