import { configureStore } from '@reduxjs/toolkit';
import ShiftPeriodSlice, { fetchShiftPeriods } from '../ShifPeriodsSlice';

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: { shiftPeriods: ShiftPeriodSlice.reducer } });

describe('ShiftPeriodSlice', () => {
  it('has correct initial state', () => {
    const state = createStore().getState().shiftPeriods;
    expect(state.shiftPeriods).toEqual([]);
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

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

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