import axios from 'axios';
import {
  getEmbargoedInvestigations,
  getInvestigationsByInstrumentScientist,
  getInvestigationsByUser,
  getReleasedInvestigations,
} from '../api/icat/icatPlus';
import {
  FETCH_INSTRUMENT_SCIENTIST_INVESTIGATIONS,
  FETCH_INVESTIGATIONS,
  FETCH_MY_INVESTIGATIONS,
  FETCH_RELEASED_INVESTIGATIONS,
} from '../constants/actionTypes';

export function fetchMyInvestigations(sessionId) {
  return {
    type: FETCH_MY_INVESTIGATIONS,
    payload: axios.get(getInvestigationsByUser(sessionId)),
  };
}

export function fetchEmbargoedInvestigations(sessionId) {
  return {
    type: FETCH_INVESTIGATIONS,
    payload: axios.get(getEmbargoedInvestigations(sessionId)),
  };
}

export function fetchReleasedInvestigations(sessionId) {
  return {
    type: FETCH_RELEASED_INVESTIGATIONS,
    payload: axios.get(getReleasedInvestigations(sessionId)),
  };
}

export function fetchInvestigationsAsInstrumentScientist(sessionId) {
  return {
    type: FETCH_INSTRUMENT_SCIENTIST_INVESTIGATIONS,
    payload: axios.get(getInvestigationsByInstrumentScientist(sessionId)),
  };
}

export function fetchAllInvestigations(sessionId) {
  return (dispatch) => {
    dispatch(fetchMyInvestigations(sessionId));
    dispatch(fetchReleasedInvestigations(sessionId));
    dispatch(fetchEmbargoedInvestigations(sessionId));
    dispatch(fetchInvestigationsAsInstrumentScientist(sessionId));
  };
}
