import { apiRequest } from "./client";
import { arr, normalizeServiceProject, normalizeServiceTeam } from "../utils/normalizers";

export function getServiceTeams() {
  return apiRequest("/api/service-teams").then((data) => arr(data).map(normalizeServiceTeam));
}

export function getServiceTeamDetail(id) {
  return apiRequest(`/api/service-teams/${id}`).then(normalizeServiceTeam);
}

export function getServiceProjects() {
  return apiRequest("/api/service-projects").then((data) => arr(data).map(normalizeServiceProject));
}

export function getServiceProjectDetail(id) {
  return apiRequest(`/api/service-projects/${id}`).then(normalizeServiceProject);
}

export function createServiceProject(data) {
  return apiRequest("/api/service-projects", {
    method: "POST",
    data
  });
}

export function createServiceTeam(data) {
  return apiRequest("/api/service-teams", {
    method: "POST",
    data
  });
}

export function updateServiceProject(id, data) {
  return apiRequest(`/api/service-projects/${id}`, {
    method: "PUT",
    data
  });
}

export function resubmitServiceProject(id) {
  return apiRequest(`/api/service-projects/${id}/resubmit`, {
    method: "POST"
  });
}

export function createServiceDemand(teamId, data = {}) {
  return apiRequest(`/api/service-teams/${teamId}/demands`, { method: "POST", data });
}
