import axios from 'axios';
import { Cookies } from 'react-cookie';
import Router from 'next/router';
import moment from 'moment';

import * as actionTypes from '../constants/actionTypes';
import * as currentState from '../reducers/initialState';

export const getUsers = ({ dispatch }) => 
{
	console.log("ACTION", "getUsers()");

	return new Promise((resolve, reject) => 
	{
		axios.post(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/admin/`, {})
		.then(async (response) => 
		{
			console.log("getContractRules", "response.data", response.data);
			dispatch({ type: actionTypes.ADMIN, data: { users: response.data.users } });

			resolve();
		})
		.catch((error) => 
		{
			console.error("ACTION", "getUsers()", "error");
			console.error(error);

			reject();
		});
	});
}

export const sendInvite = ({ name, email, companies }) => 
{
	console.log("ACTION", "sendInvite()", { name, email, companies });

	return new Promise((resolve, reject) => 
	{
		axios.post(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/admin/invite`, { name, email, companies })
		.then(async (response) => 
		{
			console.log("ACTION", "sendInvite()", "response.data", response.data);

			resolve(response.data);
		})
		.catch((error) => 
		{
			console.error("ACTION", "sendInvite()", "error");
			console.log("error.response.data", error.response.data);

			reject(error.response.data);
		});
	});
}

export const removeUser = ({ account, emails }) => 
{
	console.log("ACTION", "removeUser()", { account, emails });

	return new Promise((resolve, reject) => 
	{
		axios.post(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/admin/remove`, { account, emails })
		.then(async (response) => 
		{
			console.log("ACTION", "removeUser()", "response.data", response.data);

			resolve(response.data);
		})
		.catch((error) => 
		{
			console.error("ACTION", "removeUser()", "error");
			console.error(error);

			reject();
		});
	});
}