import { configureStore } from '@reduxjs/toolkit';
import EmployeeRequestsSlice, { fetchEmployeeRequests, fetchMyEmployeeRequests, fetchRequestTypes } from '../EmployeeRequestsSlice';

jest.mock('@/services/api/api', () => ({ apiGet: jest.fn(), apiPost: jest.fn(), apiPut: jest.fn(), apiDelete: jest.fn() }));
jest.mock('antd', () => ({ message: { success: jest.fn(), error: jest.fn(), warning: jest.fn() } }));
jest.mock('@/utilities/Helpers', () => ({ default: { handleServerError: jest.fn((e) => e?.message || 'Error') } }));

const createStore = () => configureStore({ reducer: { employeeRequests: EmployeeRequestsSlice.reducer } });

describe('EmployeeRequestsSlice', () => {
  it('has correct initial state', () => {
    const state = createStore().getState().employeeRequests;
    expect(state.employeeRequests).toEqual([]);
    expect(state.myEmployeeRequests).toEqual([]);
    expect(state.selectedEmployeeRequest).toBeNull();
    expect(state.requestTypes).toEqual([]);
    expect(state.workflows).toEqual([]);
    expect(state.dashboard).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.actionLoading).toBe(false);
    expect(state.error).toBeNull();
  });

  it('sets loading on fetchEmployeeRequests.pending', () => {
    const store = createStore();
    store.dispatch(fetchEmployeeRequests.pending('req'));
    expect(store.getState().employeeRequests.loading).toBe(true);
  });

  it('populates data on fetchEmployeeRequests.fulfilled', () => {
    const store = createStore();
    const data = [{ id: '1', status: 'draft' }] as any;
    store.dispatch(fetchEmployeeRequests.fulfilled(data, 'req'));
    expect(store.getState().employeeRequests.employeeRequests).toEqual(data);
    expect(store.getState().employeeRequests.loading).toBe(false);
  });

  it('populates my requests on fetchMyEmployeeRequests.fulfilled', () => {
    const store = createStore();
    const data = [{ id: '2', status: 'submitted' }] as any;
    store.dispatch(fetchMyEmployeeRequests.fulfilled(data, 'req'));
    expect(store.getState().employeeRequests.myEmployeeRequests).toEqual(data);
  });

  it('populates request types on fetchRequestTypes.fulfilled', () => {
    const store = createStore();
    const data = [{ id: '1', name: 'Travel' }] as any;
    store.dispatch(fetchRequestTypes.fulfilled(data, 'req'));
    expect(store.getState().employeeRequests.requestTypes).toEqual(data);
  });
});
