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


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

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

export const getUsers = async(filter) => {
    try {
        const endpoint = `/users?${filter}`;
        const {data} = await api.get(endpoint);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const createUser = async(formData) => {
    try {
        const endpoint = `/users`;
        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 = `/users/${user_id}`;
        const {data} = await api.patch(endpoint, formData);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}

export const deleteUser = async(user_id) => {
    try {
        const endpoint = `/users/${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 = `/budgets/${filter}`;
        const {data} = await api.get(endpoint);
        // console.log(data);
        return data;
    } catch (error) {
        console.log(error);
        return null;
    }
}


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

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

export const deleteBudget = async(id) => {
    try {
        const endpoint = `/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 = `/branches/${filter}&$limit=3&$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 = `/branches`;
        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 = `/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 = `/branches/${id}`;
        const {data} = await api.delete(endpoint);
        return data;
    } catch (error) {
        console.log(error);
        return 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;
//     }
// }