import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { DELETE, GET, POST } from "../../utils/api";
import { API } from "../../endpoints/common_endpoints";
import { useSelector } from "react-redux";
import Cookies from "js-cookie";

// Thunk for getting all CARTS
export const getAllWalletHistory = createAsyncThunk(
  "get-all-wallet",
  async (_, { rejectWithValue }) => {
    try {
      const response = await GET(API.Wallet);
      return response;
    } catch (error) {
      return rejectWithValue(error.message);
    }
  }
);

// Thunk for Checking itme in  CART
export const checkInWishlist = createAsyncThunk(
  "check-in-wishlist",
  async (product_id, { rejectWithValue }) => {
    try {
      const response = await GET(API.CHECk_ITEM_IN_WISHLIST + `/${product_id}`);
      return response;
    } catch (error) {
      return rejectWithValue(error.message);
    }
  }
);

// Thunk for posting CART
export const postWithdrawRequest = createAsyncThunk(
  "post-cart",
  async ({ data }, { rejectWithValue }) => {
    try {
      const response = await POST(`${API.Wallet}/withdraw-amount`, data);
      if (response?.success) {
        return response;
      }
      return rejectWithValue(response);
    } catch (error) {
      return rejectWithValue(error.message);
    }
  }
);

export const postAddMoney = createAsyncThunk(
  "add-money",
  async ({ data }, { rejectWithValue }) => {
    try {
      const response = await POST(`${API.Wallet}`, data);
      if (response?.success) {
        return response;
      }
      return rejectWithValue(response);
    } catch (error) {
      return rejectWithValue(error.message);
    }
  }
);

// Create the slice
const wishlitSlice = createSlice({
  name: "wallet",
  initialState: {
    allWalletHistory: [],
    storeInFav: false,
    error: null,
    status: "idle",
  },
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(getAllWalletHistory.pending, (state) => {
        state.status = "loading";
       
      })
      .addCase(getAllWalletHistory.fulfilled, (state, action) => {
        state.status = "succeeded";
        state.allWalletHistory = action.payload?.data || [];
        console.log(
          "getAllWishlist Response payload in fulfilled case:",
          action.payload
        );
        console.log("State after fulfilled", state);
      })
      .addCase(getAllWalletHistory.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.payload;
        console.log("State after rejected", state);
      })

      .addCase(checkInWishlist.pending, (state) => {
        state.status = "loading";
       
      })
      .addCase(checkInWishlist.fulfilled, (state, action) => {
        state.status = "succeeded";
        console.log(
          "checkInWishlist Response payload in fulfilled case:",
          action.payload.data
        );
        state.itemInWishlist = action.payload?.data?.is_wishlisted || false;
        console.log("checkInWishlist State after fulfilled", state);
      })
      .addCase(checkInWishlist.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.payload;
        console.log("State after rejected", state);
      })

      .addCase(postWithdrawRequest.pending, (state) => {
        state.status = "loading";
        
      })
      .addCase(postWithdrawRequest.fulfilled, (state, action) => {
        state.status = "succeeded";
        console.log("Response payload in fulfilled case:", action.payload);
        console.log("State after fulfilled", state);
      })
      .addCase(postWithdrawRequest.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.payload;
        console.log("State after rejected", state);
      })

        .addCase(postAddMoney.pending, (state) => {
        state.status = "loading";
       
      })
      .addCase(postAddMoney.fulfilled, (state, action) => {
        state.status = "succeeded";
        console.log("Response payload in fulfilled case:", action.payload);
        console.log("State after fulfilled", state);
      })
      .addCase(postAddMoney.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.payload;
        console.log("State after rejected", state);
      });
  },
});

export default wishlitSlice.reducer;
