import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';

const BASE_URL = import.meta.env.VITE_REACT_APP_SECRET; 

// Async thunk to fetch admin user details
export const fetchAdminUser = createAsyncThunk('admin/fetchAdminUser', async () => {
  const response = await axios.get(`${BASE_URL}/admin/username`);
  return response.data;
});

const adminSlice = createSlice({
  name: 'admin',
  initialState: {
    user: null,
    loading: false,
    error: null,
  },
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchAdminUser.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(fetchAdminUser.fulfilled, (state, action) => {
        state.loading = false;
        state.user = action.payload;
      })
      .addCase(fetchAdminUser.rejected, (state, action) => {
        state.loading = false;
        state.error = action.error.message;
      });
  },
});

export default adminSlice.reducer;
