/* eslint-disable import/order */
/* eslint-disable @typescript-eslint/consistent-type-imports */
import { UserInterface } from "@/types/userInterface";
import { createSlice, type PayloadAction } from "@reduxjs/toolkit";
import { userState } from "./type";
import { authMe } from "./thunk";

const initialState: userState = {
    status: "idle",
    error: null,
    users: [],
    userSelected: null,
    userConnected: null
};

const userSlice = createSlice({
  name: "user",
  initialState,
  reducers: {
    selectuser: (state, action: PayloadAction<UserInterface>) => {
      state.userSelected = action.payload;
    }
  },
  extraReducers: (builder) => {


  /**
   * auth me
   */
    builder.addCase(authMe.fulfilled, (state, action) => {
      
      const data = action.payload;

      state.userConnected = data;
      state.error = null;
      state.status = "succeeded";
    });

    builder.addCase(authMe.pending, (state) => {
      state.status = "loading";
    });

    builder.addCase(authMe.rejected, (state, action) => {
      state.status = "failed";
      state.error = action.error.message || "An error occurred";
    });
  },
});


export const { selectuser } = userSlice.actions
export default userSlice.reducer;


