import axios from "axios";

// helper functions for fetching, deleting and initializing gameruns
axios.defaults.withCredentials = true;

export async function fetchGameruns() {
  return await axios.get("http://localhost:8000/gameruns/").then((res) => {
    return res.data;
  });
}
export async function fetchGamerunById(gamerunID) {
  return await axios
    .get("http://localhost:8000/gameruns/" + gamerunID)
    .then((res) => {
      return res.data;
    });
}

export async function deleteGamerun(gamerun_id) {
  return await axios.delete("http://localhost:8000/gameruns/" + gamerun_id);
}

export async function initGamerun(username) {
  return await axios.post("http://localhost:8000/gamerun/init/", {
    username: username,
  });
}

/** fetch emails of current gamerun with statistics:
 *  average_correctness,
 *  average_duration,
 *  players_duration - answer duration of current player,
 *  answer - player answer (true = phishing, false = legitimate)
 */
export async function fetchCurrentGamerunSummary() {
  return await axios
    .get("http://localhost:8000/gamerun/summary/")
    .then((summary) => {
      return summary.data;
    });
}

export async function postAnswer(answer) {
  return await axios.post("http://localhost:8000/gamerun/setAnswer/", {
    answer: answer,
  });
}

export async function getNextEmail() {
  return await axios
    .get("http://localhost:8000/gamerun/nextEmail/")
    .then((data) => {
      return data;
    });
}
