import i18n from "i18next";
import { logImEXEvent } from "../../firebase/firebase.utils";
import { UPDATE_JOB } from "../../graphql/jobs.queries";
import { insertAuditTrail } from "../../redux/application/application.actions";
import { store } from "../../redux/store";
import AuditTrailMapping from "../../utils/AuditTrailMappings";

export default function AddToProduction(apolloClient, jobId, completionCallback, remove = false, notification) {
  logImEXEvent("job_add_to_production");

  //get a list of all fields on the job
  apolloClient
    .mutate({
      mutation: UPDATE_JOB,
      variables: { jobId: jobId, job: { inproduction: !remove } }
    })
    .then(() => {
      notification.success({
        title: i18n.t("jobs.successes.save")
      });

      store.dispatch(
        insertAuditTrail({
          jobid: jobId,
          operation: AuditTrailMapping.jobinproductionchange(!remove),
          type: "jobinproductionchange"
        })
      );
      if (completionCallback) completionCallback();
    })
    .catch((error) => {
      notification.error({
        title: i18n.t("jobs.errors.saving", {
          error: JSON.stringify(error)
        })
      });
    });

  //insert the new job. call the callback with the returned ID when done.
}
