import { configureStore } from '@reduxjs/toolkit';
import SalaryNotchesSlice, {
  fetchSalaryNotches,
  createSalaryNotch,
  updateSalaryNotch,
  deleteSalaryNotch,
  bulkUpdateSalaryNotches,
} from '../SalaryNotchesSlice';

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', () => {
  const mockHelpers = { handleServerError: jest.fn((e: any) => e?.message || 'Error') };
  return { __esModule: true, default: mockHelpers };
});

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

const createTestStore = () =>
  configureStore({ reducer: { salaryNotches: SalaryNotchesSlice.reducer } });

const mockNotch = { id: '1', name: 'Notch 1', amount: 2000, salary_grade_id: 'g1' };

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

  it('has correct initial state', () => {
    const store = createTestStore();
    const state = store.getState().salaryNotches;
    expect(state.salaryNotches).toEqual([]);
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

  describe('fetchSalaryNotches', () => {
    it('populates notches on fulfilled', async () => {
      apiGet.mockResolvedValue({ data: [mockNotch] });
      const store = createTestStore();
      await store.dispatch(fetchSalaryNotches());
      expect(store.getState().salaryNotches.salaryNotches).toEqual([mockNotch]);
      expect(store.getState().salaryNotches.loading).toBe(false);
    });
  });

  describe('createSalaryNotch', () => {
    it('prepends new notch on fulfilled', async () => {
      apiPost.mockResolvedValue({ data: { data: mockNotch } });
      const store = createTestStore();
      await store.dispatch(createSalaryNotch({ name: 'Notch 1' } as any));
      expect(store.getState().salaryNotches.salaryNotches[0]).toEqual(mockNotch);
    });
  });

  describe('updateSalaryNotch', () => {
    it('updates notch in list on fulfilled', async () => {
      const updated = { ...mockNotch, name: 'Notch 2' };
      apiPut.mockResolvedValue({ data: { data: updated } });
      const store = createTestStore();
      store.dispatch({ type: fetchSalaryNotches.fulfilled.type, payload: [mockNotch] });
      await store.dispatch(updateSalaryNotch({ id: '1', name: 'Notch 2' } as any));
      expect(store.getState().salaryNotches.salaryNotches[0].name).toBe('Notch 2');
    });
  });

  describe('bulkUpdateSalaryNotches', () => {
    it('sets loading on pending and clears on fulfilled', async () => {
      apiPost.mockResolvedValue({ data: { message: 'Bulk updated' } });
      apiGet.mockResolvedValue({ data: [mockNotch] });
      const store = createTestStore();
      await store.dispatch(bulkUpdateSalaryNotches({ notches: [] } as any));
      expect(store.getState().salaryNotches.loading).toBe(false);
    });
  });

  describe('deleteSalaryNotch', () => {
    it('removes notch from list on fulfilled', async () => {
      apiDelete.mockResolvedValue({ data: { id: '1', message: 'Deleted' } });
      const store = createTestStore();
      store.dispatch({ type: fetchSalaryNotches.fulfilled.type, payload: [mockNotch] });
      await store.dispatch(deleteSalaryNotch('1'));
      expect(store.getState().salaryNotches.salaryNotches).toEqual([]);
    });
  });
});
