// // for local dev
// const api_protocol_dev = "http";
// const api_host_dev = "localhost";
// const api_port_dev = "2222";
// const api_fqdn_addr = `${api_protocol_dev}://${api_host_dev}:${api_port_dev}`;

// for production
const api_protocol_production = 'https'
const api_host_production = 'api.cineshare.ch'
const api_port_production = '443'
const api_fqdn_addr = `${api_protocol_production}://${api_host_production}:${api_port_production}`

// Authentication
export const login = (username, password, role) => {
  return postJson("/auth/login", { username, password, role }).then(parseJSON);
};

export const refreshToken = (refreshToken) => {
  return postAuthenticatedJson("/auth/refresh_token", refreshToken, {}).then(
    parseJSON
  );
};

export const logout = (token, userObject) => {
  return postAuthenticatedJson("/auth/logout", token, {}).then(parseJSON);
};

// Jobs
export const getAllJobs = (token) => {
  return getAuthenticatedJson("/projects", token).then(parseJSON);
};

export const newJob = (token, jobObject) => {
  return postAuthenticatedJson("/projects", token, jobObject).then(parseJSON);
};

export const getSpecificJob = (token, userId) => {
  return getAuthenticatedJson(`/projects/${userId}`, token).then(parseJSON);
};

export const updateJob = (token, userObject, id) => {
  return putAuthenticatedJson(`/projects/${id}`, token, userObject).then(
    parseJSON
  );
};

export const deleteJob = (token, userObject, id) => {
  return deleteAuthenticatedJson(`/projects/${id}`, token, userObject).then(
    parseJSON
  );
};

// Customers
export const newCustomer = (token, userObject) => {
  return postAuthenticatedJson("/customers", token, userObject).then(parseJSON);
};

export const getAllCustomers = (token) => {
  return getAuthenticatedJson("/customers", token).then(parseJSON);
};

export const getSpecificCustomer = (token, userId) => {
  return getAuthenticatedJson(`/customers/${userId}`, token).then(parseJSON);
};

export const updateCustomer = (token, userObject, id) => {
  return putAuthenticatedJson(`/customers/${id}`, token, userObject).then(
    parseJSON
  );
};

export const deleteCustomer = (token, userObject, id) => {
  return deleteAuthenticatedJson(`/customers/${id}`, token, userObject).then(
    parseJSON
  );
};

// Employees
export const getAllEmployees = (token) => {
  return getAuthenticatedJson("/employees", token).then(parseJSON);
};

export const newEmployee = (token, userObject) => {
  return postAuthenticatedJson("/employees", token, userObject).then(parseJSON);
};

export const getSpecificEmployee = (token, userId) => {
  return getAuthenticatedJson(`/employees/${userId}`, token).then(parseJSON);
};

export const updateEmployee = (token, userObject, id) => {
  return putAuthenticatedJson(`/employees/${id}`, token, userObject).then(
    parseJSON
  );
};

export const deleteEmployee = (token, userObject, id) => {
  return deleteAuthenticatedJson(`/employees/${id}`, token, userObject).then(
    parseJSON
  );
};

// Helper functions

function parseJSON(response) {
  return response.json().then((data) => {
    return data;
  });
}

function checkStatus(response) {
  if (response.ok) {
    return response;
  } else {
    return response.json().then((data) => {
      throw new Error(data.error);
    });
  }
}

function postJson(endpoint, params) {
  return fetch(`${api_fqdn_addr}${endpoint}`, {
    method: "POST",
    credentials: "include",
    headers: {
      "Content-Type": "application/json",
      Accept: "application/json",
    },
    body: JSON.stringify(params),
  }).then(checkStatus);
}

function getAuthenticatedJson(endpoint, token) {
  return fetch(`${api_fqdn_addr}${endpoint}`, {
    method: "GET",
    credentials: "include",
    headers: {
      Authorization: `Bearer ${token}`,
      Accept: "application/json",
    },
  }).then(checkStatus);
}

function postAuthenticatedJson(endpoint, token, params) {
  return universalFetch("POST", endpoint, token, params);
}

function putAuthenticatedJson(endpoint, token, params) {
  return universalFetch("PUT", endpoint, token, params);
}

function deleteAuthenticatedJson(endpoint, token, params) {
  return universalFetch("DELETE", endpoint, token, params);
}

function universalFetch(method, endpoint, token, params) {
  return fetch(`${api_fqdn_addr}${endpoint}`, {
    method: method,
    credentials: "include",
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json",
      Accept: "application/json",
    },
    body: JSON.stringify(params),
  }).then(checkStatus);
}
