import axios from 'axios'
import { EducatorRegistrationActionTypes } from './educator.action.types'
import {METADATA_END_POINTS} from '../helpers/action-urls'
import {MettaSvr_Common} from '../helpers/action-enums'

// register user state changes
const postEducatorStart = () => ({
    type: EducatorRegistrationActionTypes.POST_EDUCATOR_START
})


const postEducatorSuccess = response => ({
    type: EducatorRegistrationActionTypes.POST_EDUCATOR_SUCCESS,
    payload: response
})

export const postEducatorFailure = err => ({
    type: EducatorRegistrationActionTypes.POST_EDUCATOR_FAILURE,
    payload: err
})

export const setEducatorRegistrationCurrentPage = (pageNum) => ({
    type: EducatorRegistrationActionTypes.POST_EDUCATOR_PAGE_CHANGE,
    payload: pageNum
})

export const clearEducatorRegistrationState = () => ({
    type: EducatorRegistrationActionTypes.POST_EDUCATOR_CLEAR_STATE
})

export const registerEducatorAsync = (body) => {
    return async dispatch => {
        dispatch(postEducatorStart())
        try{

            // dispatch(postEducatorFailure({message: 'very serious erorr 400!!', apiResponseStatus: 1}))
            // dispatch(postUserSuccess({
            //     "result": "58f88e59-58b5-4885-9a80-7bd1eca7b3e0",
            //     "orderDetails": null,
            //     "apiResponseStatus": 3,
            //     "errorMessage": null
            // }))

            // return
            const { API_VERSION_V1, REGISTER_ENDPOINT} = METADATA_END_POINTS.auth
            const response = await axios.post(API_VERSION_V1 + REGISTER_ENDPOINT, body)
            const {apiResponseStatus, errorMessage, title, result} = response.data
            if (apiResponseStatus === MettaSvr_Common.APIResponseStatus.Error){
                dispatch(postEducatorFailure({message: errorMessage ?? title, apiResponseStatus}))
            }else{
                dispatch(postEducatorSuccess(result))
            }
        }catch(ex){            
            console.log("Error is: ", !!ex.response ? ex.response.data.errors : 'none')
            dispatch(postEducatorFailure({message: ex.message ?? ex.response.data.title, apiResponseStatus: -1}))

        }
    }
}