import { IUserCallbacks } from "@/api/resources/UserCallbacks";
import { createSlice, createSelector } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";

const initialState: IUserCallbacks = {
  id: "",
  user_id: 0,
  kind: "",
  created_at: "",
  updated_at: "",
  prev_stat_changes: [],
  description: "",
  is_complete: false,
};

const userCallbacksSlice = createSlice({
  name: "userCallbacks",
  initialState,
  reducers: {
    update(state, action: PayloadAction<Partial<IUserCallbacks>>) {
      return { ...state, ...action.payload };
    },
  },
  extraReducers: (builder) => builder.addCase("reset", () => initialState),
});

export const { actions } = userCallbacksSlice;
export const selectUserCallbacksDescription = createSelector(
  (state: { userCallbacks: IUserCallbacks }) => state.userCallbacks.description,
  (userCallbacks) => userCallbacks
);
export const selectUserCallbacksPrevStat = createSelector(
  (state: { userCallbacks: IUserCallbacks }) => state.userCallbacks.prev_stat_changes,
  (userCallbacks) => userCallbacks
);
export default userCallbacksSlice.reducer;
