import {
  addQueryParams, errorHandler, fetchRespHandler, getAuthHeaderOptions
} from "../utils/http";

const API_ENDPOINT = process.env.REACT_APP_OTA_SERVICE_URL;

const fleetsAPI = {
  addFleet: async (fleet, token) =>
    fetch(`${API_ENDPOINT}/fleet`, {
      method: "POST",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
      body: JSON.stringify(fleet),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  getFleet: async (name, token) =>
    fetch(`${API_ENDPOINT}/fleet/${name}`, {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  getFleets: async (search, token, controller) =>
    fetch(addQueryParams(`${API_ENDPOINT}/fleets`, search), {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
      signal: controller?.signal,
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  updateFleet: async (name, fleet, token) =>
    fetch(`${API_ENDPOINT}/fleet/${name}`, {
      method: "PUT",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
      body: JSON.stringify(fleet),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  deleteFleet: async (name, token) =>
    fetch(`${API_ENDPOINT}/fleet/${name}`, {
      method: "DELETE",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  getFleetVehicles: async (name, search, token) =>
    fetch(addQueryParams(`${API_ENDPOINT}/fleet/${name}/vehicles`, search), {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  getFleetVINs: async (name, token) =>
    fetch(`${API_ENDPOINT}/fleet/${name}/vins`, {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  addFleetVehicles: async (name, payload, token) =>
    fetch(`${API_ENDPOINT}/fleet/${name}/vehicles/add`, {
      method: "POST",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
      body: JSON.stringify(payload),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  deleteFleetVehicles: async (name, vins, token) =>
    fetch(`${API_ENDPOINT}/fleet/${name}/vehicles/delete`, {
      method: "POST",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
      body: JSON.stringify({ vins }),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  getFleetCANFilters: async (name, search, token) =>
    fetch(addQueryParams(`${API_ENDPOINT}/fleet/${name}/filters`, search), {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  addFleetCANFilter: async (name, filter, token) =>
    fetch(`${API_ENDPOINT}/fleet/${name}/filter`, {
      method: "POST",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
      body: JSON.stringify(filter),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  updateFleetCANFilter: async (name, can_id, filter, token) =>
    fetch(`${API_ENDPOINT}/fleet/${name}/filter/${can_id}`, {
      method: "PUT",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
      body: JSON.stringify(filter),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  deleteFleetCANFilter: async (name, can_id, token) =>
    fetch(`${API_ENDPOINT}/fleet/${name}/filter/${can_id}`, {
      method: "DELETE",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),

  getFlashpackVersionInfoFleet: async (name, token) =>
    fetch(`${API_ENDPOINT}/flashpack_version_info_fleet/${name}`, {
      method: "GET",
      headers: Object.assign(
        { "Content-Type": "application/json" },
        getAuthHeaderOptions(token)
      ),
    })
      .then(fetchRespHandler)
      .catch(errorHandler),
};

export default fleetsAPI;
