// fundDetailsSlice.js
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';

export const fetchFundDetails = createAsyncThunk(
  'fundDetails/fetchFundDetails',
  async (id, { rejectWithValue }) => {
    try {
      const response = await axios.get(`${import.meta.env.VITE_REACT_APP_SECRET}/properties/${id}/fund-details`);
      return response.data.fundDetails;
    } catch (error) {
      return rejectWithValue(error.response.data);
    }
  }
);

const fundDetailsSlice = createSlice({
  name: 'fundDetails',
  initialState: {
    fundDetails: [],
    loading: false,
    error: null,
  },
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchFundDetails.pending, (state) => {
        state.loading = true;
      })
      .addCase(fetchFundDetails.fulfilled, (state, action) => {
        state.loading = false;
        state.fundDetails = action.payload;
      })
      .addCase(fetchFundDetails.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload;
      });
  },
});

export const selectFundDetails = (state) => state.fundDetails.fundDetails;
export const selectLoading = (state) => state.fundDetails.loading;
export const selectError = (state) => state.fundDetails.error;

export default fundDetailsSlice.reducer;
