import Vue from 'vue';
import axios from 'axios';

const API_URL = 'http://localhost:5443/api';

export function fetchSurveys() {
    return axios.get(`${API_URL}/surveys/`);
}

export function fetchSurvey(surveyId: string) {
    return axios.get(`${API_URL}/surveys/${surveyId}/`);
}

export function saveSurveyResponse(surveyResponse: any) {
    return axios.put(`${API_URL}/surveys/${surveyResponse.id}/`, surveyResponse);
}

export function postNewSurvey(survey: any, jwt: any) {
    return axios.post(`${API_URL}/surveys/`, survey, { headers: { Authorization: `Bearer ${jwt}` } });
}

export function authenticate(userData: any) {
    return axios.post(`${API_URL}/auth/login`, userData);
}

export function revokeRefreshKey(jwt: any) {
    //return axios.post(`${API_URL}/auth/revokeRefreshToken`, { headers: { Authorization: `Bearer ${jwt}` } });
    return axios.get(`${API_URL}/auth/revokeRefreshToken`, { headers: { Authorization: `Bearer ${jwt}` } });
}

export function register(userData: any) {
    return axios.post(`${API_URL}/auth/register`, userData);
}

// This function probably isn't really useful here, as a user must be redirected to the actual OIDC provider.
// -> So login involves user interaction through the frontend (and not just API calls).
export function oidc_login(redirectionUrl: any) {
    return axios.get(`${API_URL}/auth/oidc`, redirectionUrl);
}

export function getFreshToken(refreshToken: any) {
    return axios.get(`${API_URL}/auth/refresh`, { headers: { Authorization: `Bearer ${refreshToken}` } });
}

export function getProviders() {
    return axios.get(`${API_URL}/auth/providers`);
}

export function fetchUsers(jwt: any) {
    return axios.get(`${API_URL}/v1/user`, { headers: { Authorization: `Bearer ${jwt}` } });
}

export function createUser(jwt: any, userData: any) {
    return axios.post(`${API_URL}/v1/user`, userData, { headers: { Authorization: `Bearer ${jwt}` } });
}

export function fetchUserGroups(jwt: any) {
    return axios.get(`${API_URL}/v1/group`, { headers: { Authorization: `Bearer ${jwt}` } });
}

export function fetchUserGroup(jwt: any, groupId: any) {
    return axios.get(`${API_URL}/v1/group/${groupId}`, { headers: { Authorization: `Bearer ${jwt}` } });
}

export function fetchProfile(jwt: any) {
    return axios.get(`${API_URL}/v1/user/profile`, { headers: { Authorization: `Bearer ${jwt}` } });
}


