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 || [];
      })
      .addCase(getAllWalletHistory.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.payload;
      })

      .addCase(checkInWishlist.pending, (state) => {
        state.status = "loading";
      })
      .addCase(checkInWishlist.fulfilled, (state, action) => {
        state.status = "succeeded";

        state.itemInWishlist = action.payload?.data?.is_wishlisted || false;
      })
      .addCase(checkInWishlist.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.payload;
      })

      .addCase(postWithdrawRequest.pending, (state) => {
        state.status = "loading";
      })
      .addCase(postWithdrawRequest.fulfilled, (state, action) => {
        state.status = "succeeded";
      })
      .addCase(postWithdrawRequest.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.payload;
      })

      .addCase(postAddMoney.pending, (state) => {
        state.status = "loading";
      })
      .addCase(postAddMoney.fulfilled, (state, action) => {
        state.status = "succeeded";
      })
      .addCase(postAddMoney.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.payload;
      });
  },
});

export default wishlitSlice.reducer;
