import { configureStore } from '@reduxjs/toolkit';
import ShiftSlice, { fetchShifts, fetchGeneratedShifts } from '../ShiftSlice';

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: { shifts: ShiftSlice.reducer } });

describe('ShiftSlice', () => {
  it('has correct initial state', () => {
    const state = createStore().getState().shifts;
    expect(state.shifts).toEqual([]);
    expect(state.generatedShifts).toEqual([]);
    expect(state.generatedShift).toBeNull();
    expect(state.selectedDateShifts).toEqual([]);
    expect(state.shift).toBeNull();
    expect(state.constrainable).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
    expect(state.publishing).toBe(false);
  });

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

  it('populates shifts on fetchShifts.fulfilled', () => {
    const store = createStore();
    const data = [{ id: '1', date: '2026-03-01' }] as any;
    store.dispatch(fetchShifts.fulfilled(data, 'req', undefined));
    expect(store.getState().shifts.shifts).toEqual(data);
    expect(store.getState().shifts.loading).toBe(false);
  });

  it('populates generatedShifts on fetchGeneratedShifts.fulfilled', () => {
    const store = createStore();
    const data = [{ id: 'gs1', name: 'March Shift' }] as any;
    store.dispatch(fetchGeneratedShifts.fulfilled(data, 'req'));
    expect(store.getState().shifts.generatedShifts).toEqual(data);
  });
});
