'use client';

import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import { getEmployee, getEmployees, updateEmployeeData, getDesignation } from './service';
import { LIST_META } from '@/utills/constant';
import type { EmployeeState } from './interface';
import { removeEmptyKeys } from '@/utills/helper';

// Define an initial state
const initialState: EmployeeState = {
  employees: [],
  designation: [],
  employeesMeta: LIST_META,
  listLoading: true,
  employee: null,
  status: 'loading',
  error: null,
};

export const fetchEmployees = createAsyncThunk(
  'employees/fetchEmployees',
  async(payload: any) => {
    const queryString = removeEmptyKeys(payload);
    const employees = await getEmployees(queryString);
    return employees;
  }
);
export const fetchDesignation = createAsyncThunk('designation/fetchDesignation', async() => {
  const response = await getDesignation(); // Assuming getGroups returns an AxiosResponse

  return response;
});
export const fetchEmployee = createAsyncThunk(
  'employees/fetchEmployee',
  async(payload: { id: number }) => {
    const employees = await getEmployee(payload.id);
    return employees;
  }
);

export const updateEmployee = createAsyncThunk(
  'employees/updateEmployee',
  async({ employeeId, employeeData }: { employeeId: number; employeeData: any }) => {
    const updatedEmployee = await updateEmployeeData(employeeId, employeeData);
    return updatedEmployee;
  }
);

const employeeSlice = createSlice({
  name: 'employees',
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchDesignation.pending, (state) => {
        state.status = 'loading';
      })
      .addCase(fetchDesignation.fulfilled, (state, action) => {
        state.status = 'succeeded';
        state.designation = action.payload.data.data;
      })
      .addCase(fetchDesignation.rejected, (state, action) => {
        state.status = 'failed';
        state.error = action.error.message ?? 'An error occurred'; // Handle undefined gracefully
      })
      .addCase(fetchEmployees.pending, (state) => {
        state.status = 'loading';
        state.listLoading = true;
      })
      .addCase(fetchEmployees.fulfilled, (state: EmployeeState, action: any) => {
        state.status = 'succeeded';
        state.listLoading = false;
        state.employees = action.payload.data;

        state.employeesMeta = action.payload.meta;
      })
      .addCase(fetchEmployees.rejected, (state: EmployeeState, action: any) => {
        state.status = 'failed';
        state.listLoading = false;
        state.error = action.error.message;
      })

      .addCase(fetchEmployee.pending, (state) => {
        state.status = 'loading';
      })
      .addCase(fetchEmployee.fulfilled, (state: EmployeeState, action: any) => {
        state.status = 'succeeded';
        state.employee = action.payload.data;
      })
      .addCase(fetchEmployee.rejected, (state: EmployeeState, action: any) => {
        state.status = 'failed';
        state.error = action.error.message;
      });
  },
});

export const selectEmployeeList = (state: any): void => state.employee.employeeList;

export default employeeSlice.reducer;
