import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import { getConfiguration } from './service';
import type { ConfigurationData } from './interface';
// Define types for group data and params
// Define types for group data and params

// Define an initial state
const initialState = {
  configuration: [] as ConfigurationData[],
  status: 'loading',
  error: '',
  meta: { current_page: '', per_page: '', total: 0 }, // Add pagination property
};

// Simulate fetching group data from your service
// slice.ts
export const fetchConfiguration = createAsyncThunk(
  'configuration/fetchConfiguration',
  async(searchQuery: any) => {
    const response = await getConfiguration(searchQuery); // Assuming getGroups returns an AxiosResponse
    return response;
  }
);

const configurationSlice = createSlice({
  name: 'configuration',
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchConfiguration.pending, (state) => {
        state.status = 'loading';
      })
      .addCase(fetchConfiguration.fulfilled, (state, action) => {
        state.status = 'succeeded';
        state.configuration = action.payload.data.data;
        state.meta = action.payload.data.meta;
      })
      .addCase(fetchConfiguration.rejected, (state, action) => {
        state.status = 'failed';
        state.error = action.error.message ?? 'An error occurred'; // Handle undefined gracefully
      });
  },
});

export default configurationSlice.reducer;
