import api from "./client";

export const StagesApi = {
  createStage(formId, payload) {
    return api.post(`/stages/form/${formId}`, payload).then((r) => r.data);
  },
  updateStage(payload) {
    const { budget_form_id, sheet, phase_code } = payload;

    return api
      .patch(`/stages/form/${budget_form_id}/${sheet}/${phase_code}`, payload)
      .then((r) => r.data);
  },
  deleteStage(formId, sheet, phaseCode) {
    return api
      .delete(`/stages/form/${formId}/${sheet}/${phaseCode}`)
      .then((r) => r.data);
  },
  getStages(formId) {
    return api.get(`/stages/form/${formId}`).then((r) => r.data);
  },
  createStageAccessHeader(tableId, stageId, payload) {
    return api
      .post(
        `/table-data/templates/templates/${tableId}/stages/${stageId}/access_headers/`,
        payload,
      )
      .then((r) => r.data);
  },
  createStageAccessCell(tableId, stageId, payload) {
    return api
      .post(
        `/table-data/templates/templates/${tableId}/stages/${stageId}/access_cells/`,
        payload,
      )
      .then((r) => r.data);
  },
  updateStageAccessHeader(tableId, stageId, payload) {
    return api
      .put(
        `/table-data/templates/templates/${tableId}/stages/${stageId}/access_headers/${payload.id}/`,
        payload,
      )
      .then((r) => r.data);
  },
  updateStageAccessCell(tableId, stageId, payload) {
    return api
      .put(
        `/table-data/templates/templates/${tableId}/stages/${stageId}/access_cells/${payload.id}/`,
        payload,
      )
      .then((r) => r.data);
  },
  deleteStageAccessHeader(tableId, stageId, accessHeaderId) {
    return api
      .delete(
        `/table-data/templates/templates/${tableId}/stages/${stageId}/access_headers/${accessHeaderId}/`,
      )
      .then((r) => r.data);
  },
  getStageAccessHeader(template_id, stage_id) {
    return api
      .get(
        `/table-data/templates/templates/${template_id}/stages/${stage_id}/access_headers/`,
      )
      .then((r) => r.data);
  },
  getStageAccessCell(template_id, stage_id) {
    return api
      .get(
        `/table-data/templates/templates/${template_id}/stages/${stage_id}/access_cells/`,
      )
      .then((r) => r.data);
  },
  deleteStageAccessCell(tableId, stageId, accessCellId) {
    return api
      .delete(
        `/table-data/templates/templates/${tableId}/stages/${stageId}/access_cells/${accessCellId}/`,
      )
      .then((r) => r.data);
  },
  getStagesForTask(taskId) {
    return api.get(`/tasks/${taskId}/stages`).then((r) => r.data);
  },
};
