import { notification } from "antd";
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
) {
  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((res) => {
      notification["success"]({
        message: i18n.t("jobs.successes.save"),
      });

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

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

  return;
}
