import { configureStore } from '@reduxjs/toolkit';
import PensionAllocationProfilesSlice, {
  fetchPensionAllocationProfiles,
  createPensionAllocationProfile,
  updatePensionAllocationProfile,
  deletePensionAllocationProfile,
} from '../PensionAllocationProfilesSlice';

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: { pensionAllocationProfiles: PensionAllocationProfilesSlice.reducer } });

const mockProfile = { id: '1', name: 'Default Profile', country_code: 'GH' };

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

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

  describe('fetchPensionAllocationProfiles', () => {
    it('populates profiles from array response', async () => {
      apiGet.mockResolvedValue({ data: [mockProfile] });
      const store = createTestStore();
      await store.dispatch(fetchPensionAllocationProfiles());
      expect(store.getState().pensionAllocationProfiles.pensionAllocationProfiles).toEqual([mockProfile]);
    });

    it('handles paginated response format', async () => {
      apiGet.mockResolvedValue({ data: { data: [mockProfile] } });
      const store = createTestStore();
      await store.dispatch(fetchPensionAllocationProfiles());
      expect(store.getState().pensionAllocationProfiles.pensionAllocationProfiles).toEqual([mockProfile]);
    });
  });

  describe('createPensionAllocationProfile', () => {
    it('prepends new profile on fulfilled', async () => {
      apiPost.mockResolvedValue({ data: { data: mockProfile } });
      const store = createTestStore();
      await store.dispatch(createPensionAllocationProfile({ name: 'Default' } as any));
      expect(store.getState().pensionAllocationProfiles.pensionAllocationProfiles[0]).toEqual(mockProfile);
    });
  });

  describe('updatePensionAllocationProfile', () => {
    it('updates profile in list on fulfilled', async () => {
      const updated = { ...mockProfile, name: 'Custom' };
      apiPut.mockResolvedValue({ data: { data: updated } });
      const store = createTestStore();
      store.dispatch({ type: fetchPensionAllocationProfiles.fulfilled.type, payload: [mockProfile] });
      await store.dispatch(updatePensionAllocationProfile({ id: '1', name: 'Custom' } as any));
      expect(store.getState().pensionAllocationProfiles.pensionAllocationProfiles[0].name).toBe('Custom');
    });
  });

  describe('deletePensionAllocationProfile', () => {
    it('removes profile by id on fulfilled', async () => {
      apiDelete.mockResolvedValue({});
      const store = createTestStore();
      store.dispatch({ type: fetchPensionAllocationProfiles.fulfilled.type, payload: [mockProfile] });
      await store.dispatch(deletePensionAllocationProfile('1'));
      expect(store.getState().pensionAllocationProfiles.pensionAllocationProfiles).toEqual([]);
    });
  });
});
