import { User } from "@/types";

export const getUsers = async (token : string) => {
    return await fetch('/api/user', {
        method : 'GET',
        headers : {
            'Accept' : 'application/json',
            'Authorization': `Bearer ${token}`
        }
    });
}

export const getUserById = async (id : number, token : string) => {
    return await fetch(`/api/user/${id}`, {
        method : 'GET',
        headers : {
            'Accept' : 'application/json',
            'Authorization': `Bearer ${token}`
        }
    });
}

export const saveUser = async (user: User, token : string) => {
    if(user.id){
        return await fetch(`/api/user/${user.id}`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(user)
        });
    } else {
        return await fetch('/api/user', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(user)
        });
    }
}

export const login = async (user: User) => {
        return await fetch('/api/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
            },
            body: JSON.stringify(user)
        });
}

