import { configureStore } from '@reduxjs/toolkit';
import IncrementalJumpsSlice, {
  fetchIncrementalJumpPreview,
  processIncrementalJump,
  fetchIncrementalJumpHistory,
  clearIncrementalJumpPreview,
} from '../IncrementalJumpsSlice';

jest.mock('@/services/api/api', () => ({
  apiGet: jest.fn(),
  apiPost: jest.fn(),
}));

jest.mock('antd', () => ({
  message: { success: jest.fn(), error: jest.fn(), warning: jest.fn() },
}));

jest.mock('@/utilities/Helpers', () => {
  const mockHelpers = { handleServerError: jest.fn((e: any) => e?.message || 'Error') };
  return { __esModule: true, default: mockHelpers };
});

const { apiGet, apiPost } = require('@/services/api/api');

const createTestStore = () =>
  configureStore({ reducer: { incrementalJumps: IncrementalJumpsSlice.reducer } });

describe('IncrementalJumpsSlice', () => {
  beforeEach(() => jest.clearAllMocks());

  it('has correct initial state', () => {
    const store = createTestStore();
    const state = store.getState().incrementalJumps;
    expect(state.preview).toBeNull();
    expect(state.history).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.processing).toBe(false);
    expect(state.error).toBeNull();
  });

  describe('fetchIncrementalJumpPreview', () => {
    it('populates preview on fulfilled', async () => {
      const preview = { eligible: [], ineligible: [], summary: {} };
      apiGet.mockResolvedValue({ data: preview });
      const store = createTestStore();
      await store.dispatch(fetchIncrementalJumpPreview());
      expect(store.getState().incrementalJumps.preview).toEqual(preview);
      expect(store.getState().incrementalJumps.loading).toBe(false);
    });
  });

  describe('processIncrementalJump', () => {
    it('sets processing on pending and clears on fulfilled', async () => {
      const result = { message: 'Processed', count: 5 };
      apiPost.mockResolvedValue({ data: result });
      const store = createTestStore();
      const promise = store.dispatch(processIncrementalJump({ effective_date: '2026-01-01' } as any));
      expect(store.getState().incrementalJumps.processing).toBe(true);
      await promise;
      expect(store.getState().incrementalJumps.processing).toBe(false);
    });
  });

  describe('fetchIncrementalJumpHistory', () => {
    it('populates history on fulfilled', async () => {
      const history = { data: [], total: 0 };
      apiGet.mockResolvedValue({ data: history });
      const store = createTestStore();
      await store.dispatch(fetchIncrementalJumpHistory());
      expect(store.getState().incrementalJumps.history).toEqual(history);
    });
  });

  describe('clearIncrementalJumpPreview', () => {
    it('clears preview', () => {
      const store = createTestStore();
      store.dispatch({
        type: fetchIncrementalJumpPreview.fulfilled.type,
        payload: { eligible: [] },
      });
      store.dispatch(clearIncrementalJumpPreview());
      expect(store.getState().incrementalJumps.preview).toBeNull();
    });
  });
});
