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,
  });
}

/** fetches emails of current gamerun with statistics: <br/>
 *
 *    * average_correctness,
 *    * average_duration,
 *    * players_duration - answer duration of current player,
 *    * answer - answer contains player answer (true = phishing, false = legitimate) and correctness (true = player's answer is correct, otherwise false)
 */
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;
    });
}
