import { configureStore } from '@reduxjs/toolkit';
import SalaryAdjustmentsSlice, {
  fetchSalaryAdjustments,
  createSalaryAdjustment,
  updateSalaryAdjustment,
  deleteSalaryAdjustment,
  approveSalaryAdjustment,
} from '../SalaryAdjustmentsSlice';

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: { salaryAdjustments: SalaryAdjustmentsSlice.reducer } });

const mockAdjustment = { id: '1', employee_id: 'e1', amount: 500, status: 'pending' };

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

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

  describe('fetchSalaryAdjustments', () => {
    it('populates adjustments on fulfilled', async () => {
      apiGet.mockResolvedValue({ data: [mockAdjustment] });
      const store = createTestStore();
      await store.dispatch(fetchSalaryAdjustments());
      expect(store.getState().salaryAdjustments.adjustments).toEqual([mockAdjustment]);
    });
  });

  describe('createSalaryAdjustment', () => {
    it('prepends new adjustment with default status on fulfilled', async () => {
      const noStatus = { id: '2', employee_id: 'e1', amount: 500 };
      apiPost.mockResolvedValue({ data: { data: noStatus } });
      const store = createTestStore();
      await store.dispatch(createSalaryAdjustment({ employee_id: 'e1' } as any));
      expect(store.getState().salaryAdjustments.adjustments[0].status).toBe('pending');
    });
  });

  describe('updateSalaryAdjustment', () => {
    it('updates adjustment in list on fulfilled', async () => {
      const updated = { ...mockAdjustment, amount: 700 };
      apiPut.mockResolvedValue({ data: { data: updated } });
      const store = createTestStore();
      store.dispatch({ type: fetchSalaryAdjustments.fulfilled.type, payload: [mockAdjustment] });
      await store.dispatch(updateSalaryAdjustment({ id: '1', amount: 700 } as any));
      expect(store.getState().salaryAdjustments.adjustments[0].amount).toBe(700);
    });
  });

  describe('deleteSalaryAdjustment', () => {
    it('removes adjustment from list on fulfilled', async () => {
      apiDelete.mockResolvedValue({ data: { id: '1', message: 'Deleted' } });
      const store = createTestStore();
      store.dispatch({ type: fetchSalaryAdjustments.fulfilled.type, payload: [mockAdjustment] });
      await store.dispatch(deleteSalaryAdjustment('1'));
      expect(store.getState().salaryAdjustments.adjustments).toEqual([]);
    });
  });

  describe('approveSalaryAdjustment', () => {
    it('updates adjustment status on fulfilled', async () => {
      const approved = { ...mockAdjustment, status: 'approved' };
      apiPost.mockResolvedValue({ data: { data: approved } });
      const store = createTestStore();
      store.dispatch({ type: fetchSalaryAdjustments.fulfilled.type, payload: [mockAdjustment] });
      await store.dispatch(approveSalaryAdjustment('1'));
      expect(store.getState().salaryAdjustments.adjustments[0].status).toBe('approved');
    });
  });
});
