import axios from "../../axios/axiosInstance";
import {
    CURRENT_USER,
    FETCH_APPLICATIONS_BY_JOB_ID,
    FETCH_APPLICATIONS_BY_JOB_SEEKER_ID,
    SUBMIT_APPLICATION, UPDATE_APPLICATION_STATUS
} from "../actionTypes";

export const ApplicationActions = {
    submitApplication: (application, callback) => {
        return dispatch => {
            axios.post("/applications/submit", application, {
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
                .then(response => {
                    dispatch({
                        type: SUBMIT_APPLICATION,
                        application: response.data
                    })
                    callback(true, response)
                }).catch(error => {
                callback(false, error)
                console.log(error)
            })
        }
    },
    updateApplicationStatus: (id, status, callback) => {
        console.log(status)
        return dispatch => {
            axios.post("/applications/" + id + "/update", {
                id: id,
                status: status
            })
                .then(response => {
                    dispatch({
                        type: UPDATE_APPLICATION_STATUS,
                        application: response.data,
                    })
                    callback(true, response)
                }).catch(error => {
                callback(false, error)
            })
        }
    },
    fetchApplicationsByJobSeeker: (jobSeekerId, callback) => {
        return dispatch => {
            axios.get("/my-applications/" + jobSeekerId)
                .then(response => {
                    dispatch({
                        type: FETCH_APPLICATIONS_BY_JOB_SEEKER_ID,
                        applicationsByJobSeeker: response.data
                    })
                    callback(true, response)
                }).catch(error => {
                callback(false, error)
            })
        }
    },

    fetchApplicationsByJobAdId: (jobAdId, callback) => {
        return dispatch => {
            axios.get("/job-advertisements/" + jobAdId + "/applications")
                .then(response => {
                        dispatch({
                            type: FETCH_APPLICATIONS_BY_JOB_ID,
                            applicationsByJobAdId: response.data
                        })
                        callback(true, response)
                    }
                ).catch(error => {
                callback(false, error)
            })
        }
    },
    downloadResume: (id, callback) => {
        return axios.get("/applications/" + id + "/download-resume", {responseType: "blob"})
            .then(response => {
                const blob = new Blob([response.data], {type: 'application/pdf'});
                const url = window.URL.createObjectURL(blob);
                callback(true, url);
            })
            .catch(error => {
                callback(false, error)
            })

    }
}