// // redux/userSlice.ts
// import { createSlice, createAsyncThunk, PayloadAction } from "@reduxjs/toolkit";
// import { RootState } from "./store";
// import { fetchMe } from "@/record-management/services/Recordservice";

// interface UserDetails {
//   id: string;
//   userName: string;
//   userDisplayName: string;
//   userPermissions: string[];
//   avator: string | null;
//   displayname: string;
// }

// const initialState: UserState = {
//   userDetails: {
//     id: "",
//     userName: "",
//     userDisplayName: "",
//     userPermissions: [],
//     avator: null,
//     displayname: "",
//   },
//   status: "idle",
// };

// interface UserState {
//   userDetails: UserDetails | null;
//   status: "idle" | "loading" | "failed";
// }

// export const fetchUserDetails = createAsyncThunk<
//   UserDetails,
//   void,
//   { rejectValue: string }
// >("user/fetchUserDetails", async (_, thunkAPI) => {
//   try {
//     const me = await fetchMe();

//     const permissions =
//       me.employee?.flatMap((emp: any) =>
//         emp.positions.flatMap((pos: any) =>
//           pos.permissions.map((p: any) => p.key)
//         )
//       ) ?? [];

//     return {
//       id: me.id,
//       userName: me.username,
//       userDisplayName: me.name.en,
//       userPermissions: permissions,
//       avator: me.avator ?? null,
//       displayname: me.name?.en ?? "",
//     };
//   } catch (error) {
//     console.error(error);
//     return thunkAPI.rejectWithValue("Failed to fetch user details");
//   }
// });

// const userSlice = createSlice({
//   name: "user",
//   initialState,
//   reducers: {
//     clearUser(state) {
//       state.userDetails = null;
//       state.status = "idle";
//     },
//   },
//   extraReducers: (builder) => {
//     builder
//       .addCase(fetchUserDetails.pending, (state) => {
//         state.status = "loading";
//       })
//       .addCase(
//         fetchUserDetails.fulfilled,
//         (state, action: PayloadAction<UserDetails>) => {
//           state.userDetails = action.payload;
//           state.status = "idle";
//         }
//       )
//       .addCase(fetchUserDetails.rejected, (state) => {
//         state.status = "failed";
//       });
//   },
// });

// export const { clearUser } = userSlice.actions;

// export const userSelector = (state: RootState) => state.user;

// export default userSlice.reducer;
