import axios from "axios";

// helper functions for fetching, updating and deleting emails
axios.defaults.withCredentials = true;

export async function fetchEmails() {
  return await axios.get("http://localhost:8000/emails/").then((res) => {
    return res.data;
  });
}

export async function fetchEmailByID(emailID) {
  return axios.get("http://localhost:8000/emails/" + emailID).then((res) => {
    return res.data;
  });
}

//TODO toto dat to backend a vytvorit endpoint
export async function fetchEmailsByGamerunID(gamerunID) {
  return axios
    .get("http://localhost:8000/gameruns/" + gamerunID + "/emails/")
    .then((res) => {
      return res.data;
    });
}

export async function postEmail(email) {
  return await axios.post("http://localhost:8000/emails/", email);
}

export async function deleteEmail(email_id) {
  return await axios.delete("http://localhost:8000/emails/" + email_id + "/");
}

export async function fetchEmailStatistics(emailID) {
  return await axios
    .get("http://localhost:8000/emails/" + emailID + "/statistics/")
    .then((res) => {
      return res.data;
    });
}

export async function updateEmail(emailID, data) {
  return axios.put("http://localhost:8000/api/emails/" + emailID + "/", data);
}
