import {
  AUTH_USER,
  SET_USER_DATA,
  UNAUTH_USER,
  AUTH_ERROR,
  SET_CREDIT_CARDS,
  SET_DEFAULT_CARD
} from '../constants/';

import {
  resetSession,
  setSession,
  getSession
} from '../utils/api/header-config';

export const authUser = () => ({ type: AUTH_USER });
export const setUserData = (user) => ({ type: SET_USER_DATA, payload: user });
export const authError = (error) => ({ type: AUTH_ERROR, payload: error });
export const unauthUser = () => ({ type: UNAUTH_USER });
export const setCreditCards = (cards) => ({type: SET_CREDIT_CARDS, payload: cards});
export const setDefaultCard = (card) => ({ type: SET_DEFAULT_CARD, payload: card });
export const addCreditCard = (card) => ({ type: ADD_CREDIT_CARD, payload: card });

// Async actions

export const signupUser = (email, password) => {
  return (dispatch, getState, { AuthAPI }) => {
    return AuthAPI.signup(email, password).request
      .then(response => {
        console.log('signup user successfully', response);
        dispatch(authUser());
        dispatch(setUserData(response.data.user));
        setSession(response.data.user, response.data.session);
      })
      .catch( error => {
        console.log('error to signup user', error.response);
        dispatch(authError(error.response.data.error.user));
      });
  }
};

export const signinUser = (email, password) => {
  return (dispatch, getState, { AuthAPI }) => {
    return AuthAPI.signin(email, password).request
      .then(response => {
        console.log('signin user successfully', response);
        dispatch(authUser());
        dispatch(setUserData(response.data.user));
        if (response.data.cards.length > 0) {
          dispatch(setCreditCards(response.data.cards));
          const defaultCard = response.data.cards.filter(card => card.default);
          dispatch(setDefaultCard(defaultCard));
        }
        setSession(response.data.user, response.data.session);
      })
      .catch(error => {
        console.log('error to signin user', error.response);
        // dispatch(authError(error.response.error.user));
      })
  }
};

export const signoutUser = () => {
  return (dispatch, getState, { AuthAPI }) => {
    return AuthAPI.signout().request
      .then(response => {
        console.log('signout user successfully', response);
        dispatch(unauthUser())
        resetSession();
      })
      .catch(error => {
        console.log('error to signout user', error.response);
        dispatch(authError(error.response.error.user));
      })
  }
};


@@ -1621,250 +1621,8 @@
 ));%0A
-        if (response.data.cards.length %3E 0) %7B%0A          dispatch(setCreditCards(response.data.cards));%0A          const defaultCard = response.data.cards.filter(card =%3E card.default);%0A          dispatch(setDefaultCard(defaultCard));%0A        %7D%0A
     
@@ -2259,28 +2259,595 @@
 or.user));%0A      %7D)%0A  %7D%0A%7D;%0A%0A
+export const getCreditCards = () =%3E %7B%0A  return (dispatch, getState, %7B CardAPI %7D) =%3E %7B%0A    return CardAPI.index().request%0A      .then(response =%3E %7B%0A        console.log('get credit cards successfully', response);%0A        if (response.data.cards.length %3E 0) %7B%0A          dispatch(setCreditCards(response.data.cards));%0A          const defaultCard = response.data.cards.filter(card =%3E card.default);%0A          dispatch(setDefaultCard(defaultCard));%0A        %7D;%0A      %7D)%0A      .catch(error =%3E %7B%0A        console.log('error to get credit cards', error.response);%0A      %7D)%0A  %7D%0A%7D

Add getCards async-action for auth