import axios from 'axios';
axios.defaults.baseURL = process.env.REACT_APP_API_URL;

export const getRobots = async () => {
    const {request, data} = await axios.get(
        '/robots'
    );

    if (request.status === 200) {
        return data;
    }
    return [];
}

export const updateRobot = async (bearer, robot) => {
    const {request} = await axios.put(
        `robots/${robot.id}`,
        {
            ...robot
        },
        {
            headers: {
                "Authorization": `Bearer ${bearer}`
            }
        }
    )
    return request.status === 200;
}

export const createRobot = async (bearer, robot) => {
    const {request, data} = await axios.post(
        '/robots',
        {
            ...robot
        },
        {
            headers: {
                "Authorization": `Bearer ${bearer}`
            }
        }
    )
    if (request.status === 200) {
        return data;
    }
    return undefined;
}

export const deleteRobot = async (bearer, id) => {
    const {request} = await axios.delete(
        `/robots/${id}`,
        {
            headers: {
                "Authorization": `Bearer ${bearer}`
            }
        }
    )

    return request.status === 200;
}
