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

export interface EmployeeMetric {
  id: string;
  name: string;
  value: number;
}

export interface HRState {
  metrics: EmployeeMetric[];
  loading: boolean;
  error: string | null;
}

const initialState: HRState = {
  metrics: [],
  loading: false,
  error: null,
};

export const fetchHRMetrics = createAsyncThunk('hr/fetchMetrics', async () => {
  // TODO: integrate real HR API
  await new Promise(r => setTimeout(r, 300));
  return [
    { id: '1', name: 'Headcount', value: 42 },
    { id: '2', name: 'Attendance %', value: 96 },
  ] as EmployeeMetric[];
});

const hrSlice = createSlice({
  name: 'hr',
  initialState,
  reducers: {},
  extraReducers: builder => {
    builder
      .addCase(fetchHRMetrics.pending, state => {
        state.loading = true;
        state.error = null;
      })
      .addCase(fetchHRMetrics.fulfilled, (state, action: PayloadAction<EmployeeMetric[]>) => {
        state.loading = false;
        state.metrics = action.payload;
      })
      .addCase(fetchHRMetrics.rejected, (state, action) => {
        state.loading = false;
        state.error = action.error.message || 'Failed to load HR metrics';
      });
  },
});

export default hrSlice;


