import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import AttendanceRepository from '../../repositories/AttendanceRepository';
import { markAttendance } from './attedanceLogsSlice';

export const fetchEmployeeAttendanceData = createAsyncThunk(
    'employeeAttendance/fetchEmployeeAttendanceData',
    async ( {employeeId, fromDate, toDate}, thunkAPI ) =>
    {
      try {
          const response = await AttendanceRepository.getAttendanceByEmployee( employeeId, fromDate, toDate );
        // return response?.data?.filter((log) => log.checkInTime !== null && log.activity !== 0);
        return response.data
      } catch (error) {
        return thunkAPI.rejectWithValue(error.message);
      }
    }
);

  
const employeeAttendancesSlice = createSlice({
    name: 'employeeAttendance',  // Updated slice name
    initialState: {
      data: [],
      loading: false,
      error: null,
    },
    reducers: {
      setEmployeeAttendanceData: (state, action) => {
        state.data = action.payload;
      },
    },
    extraReducers: (builder) => {
      builder
        // Fetch attendance data
        .addCase(fetchEmployeeAttendanceData.pending, (state) => {
          state.loading = true;
        })
        .addCase(fetchEmployeeAttendanceData.fulfilled, (state, action) => {
            state.loading = false;
            state.data = action.payload;
          })
         
          
        .addCase(fetchEmployeeAttendanceData.rejected, (state, action) => {
          state.loading = false;
          state.error = action.payload;
        })
  
     
          
        
    },
  });
  
  export const { setEmployeeAttendanceData } = employeeAttendancesSlice.actions;
  export default employeeAttendancesSlice.reducer;