import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import type { AuthUser } from '@/modules/auth/store/authSlice';

export interface ProfileState {
  name: string;
  email: string;
  role: string;
  userId: number | null;
  uuid: string;
  loading: boolean;
  error: string | null;
}

const initialState: ProfileState = {
  name: '',
  email: '',
  role: '',
  userId: null,
  uuid: '',
  loading: false,
  error: null,
};

const profileSlice = createSlice({
  name: 'profile',
  initialState,
  reducers: {
    setProfile: (state, action: PayloadAction<{ name: string; email: string; role?: string; userId?: number; uuid?: string }>) => {
      state.name = action.payload.name;
      state.email = action.payload.email;
      if (action.payload.role) state.role = action.payload.role;
      if (action.payload.userId) state.userId = action.payload.userId;
      if (action.payload.uuid) state.uuid = action.payload.uuid;
    },
    setProfileFromAuth: (state, action: PayloadAction<AuthUser>) => {
      state.name = action.payload.displayName;
      state.email = action.payload.email;
      state.role = action.payload.role;
      state.userId = action.payload.id;
      state.uuid = action.payload.uuid;
    },
    setLoading: (state, action: PayloadAction<boolean>) => {
      state.loading = action.payload;
    },
    setError: (state, action: PayloadAction<string | null>) => {
      state.error = action.payload;
    },
    resetState: () => initialState,
  },
});

export const { setProfile, setProfileFromAuth, setLoading, setError, resetState } = profileSlice.actions;
export default profileSlice;


