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

export const markCurrentAttendance = createAsyncThunk(
    'attendanceCurrentDate/markAttendance',
    async (formData, { getState, dispatch, rejectWithValue }) => {
      const { projectId } = getState().localVariables
      try {
        // Create the new attendance record
        const newRecordAttendance = {
          id: null,
          comment: formData.description,
          employeeID: formData.employeeId,
          projectId: projectId,
          date: new Date().toISOString(),
          markTime: formData.time,
          latitude: formData.latitude.toString(),
          longitude: formData.longitude.toString(),
          action: formData.action,
          image: null,
        };
         
        const response = await AttendanceRepository.markAttendance(newRecordAttendance);
        const markedAttendance = response.data
        return markedAttendance;
  
      } catch (error) {
        console.error('Error marking attendance:', error);
        return rejectWithValue(error.message); // Reject with error message
      }
    }
  );