import { configureStore } from '@reduxjs/toolkit';
import StatutoryConfigurationsSlice, {
  fetchStatutoryConfigurations,
  createStatutoryConfiguration,
  updateStatutoryConfiguration,
  deleteStatutoryConfiguration,
} from '../StatutoryConfigurationsSlice';

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: { statutoryConfigurations: StatutoryConfigurationsSlice.reducer } });

const mockConfig = { id: '1', name: 'SSNIT', type: 'pension', rate: 5.5, is_active: true };

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

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

  describe('fetchStatutoryConfigurations', () => {
    it('populates configurations on fulfilled', async () => {
      apiGet.mockResolvedValue({ data: [mockConfig] });
      const store = createTestStore();
      await store.dispatch(fetchStatutoryConfigurations());
      expect(store.getState().statutoryConfigurations.statutoryConfigurations).toEqual([mockConfig]);
    });
  });

  describe('createStatutoryConfiguration', () => {
    it('prepends new config on fulfilled', async () => {
      apiPost.mockResolvedValue({ data: { data: mockConfig } });
      const store = createTestStore();
      await store.dispatch(createStatutoryConfiguration({ name: 'SSNIT' } as any));
      expect(store.getState().statutoryConfigurations.statutoryConfigurations[0]).toEqual(mockConfig);
    });
  });

  describe('updateStatutoryConfiguration', () => {
    it('updates config in list on fulfilled', async () => {
      const updated = { ...mockConfig, rate: 6.0 };
      apiPut.mockResolvedValue({ data: { data: updated } });
      const store = createTestStore();
      store.dispatch({ type: fetchStatutoryConfigurations.fulfilled.type, payload: [mockConfig] });
      await store.dispatch(updateStatutoryConfiguration({ id: '1', rate: 6.0 } as any));
      expect(store.getState().statutoryConfigurations.statutoryConfigurations[0].rate).toBe(6.0);
    });
  });

  describe('deleteStatutoryConfiguration', () => {
    it('removes config from list on fulfilled', async () => {
      apiDelete.mockResolvedValue({ data: { id: '1', message: 'Deleted' } });
      const store = createTestStore();
      store.dispatch({ type: fetchStatutoryConfigurations.fulfilled.type, payload: [mockConfig] });
      await store.dispatch(deleteStatutoryConfiguration('1'));
      expect(store.getState().statutoryConfigurations.statutoryConfigurations).toEqual([]);
    });
  });
});
