import { configureStore } from '@reduxjs/toolkit';
import EmployeeAllowancesSlice, {
  fetchEmployeeAllowances,
  fetchAllowanceMatrix,
  createEmployeeAllowance,
  updateEmployeeAllowance,
  deleteEmployeeAllowance,
} from '../EmployeeAllowancesSlice';

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: { employeeAllowances: EmployeeAllowancesSlice.reducer } });

const mockAllowance = { id: '1', employee_id: 'e1', allowance_type_id: 'at1', amount: 500 };

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

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

  describe('fetchEmployeeAllowances', () => {
    it('populates allowances on fulfilled', async () => {
      apiGet.mockResolvedValue({ data: [mockAllowance] });
      const store = createTestStore();
      await store.dispatch(fetchEmployeeAllowances());
      expect(store.getState().employeeAllowances.employeeAllowances).toEqual([mockAllowance]);
    });
  });

  describe('fetchAllowanceMatrix', () => {
    it('sets matrixLoading and populates matrix on fulfilled', async () => {
      const matrixData = { employees: [], types: [] };
      apiGet.mockResolvedValue({ data: matrixData });
      const store = createTestStore();
      await store.dispatch(fetchAllowanceMatrix());
      const state = store.getState().employeeAllowances;
      expect(state.matrix).toEqual(matrixData);
      expect(state.matrixLoading).toBe(false);
    });
  });

  describe('createEmployeeAllowance', () => {
    it('prepends new allowance on fulfilled', async () => {
      apiPost.mockResolvedValue({ data: { data: mockAllowance } });
      const store = createTestStore();
      await store.dispatch(createEmployeeAllowance({ employee_id: 'e1' } as any));
      expect(store.getState().employeeAllowances.employeeAllowances[0]).toEqual(mockAllowance);
    });
  });

  describe('updateEmployeeAllowance', () => {
    it('updates allowance in list on fulfilled', async () => {
      const updated = { ...mockAllowance, amount: 700 };
      apiPut.mockResolvedValue({ data: { data: updated } });
      const store = createTestStore();
      store.dispatch({ type: fetchEmployeeAllowances.fulfilled.type, payload: [mockAllowance] });
      await store.dispatch(updateEmployeeAllowance({ id: '1', amount: 700 } as any));
      expect(store.getState().employeeAllowances.employeeAllowances[0].amount).toBe(700);
    });
  });

  describe('deleteEmployeeAllowance', () => {
    it('removes allowance from list on fulfilled', async () => {
      apiDelete.mockResolvedValue({ data: { id: '1', message: 'Deleted' } });
      const store = createTestStore();
      store.dispatch({ type: fetchEmployeeAllowances.fulfilled.type, payload: [mockAllowance] });
      await store.dispatch(deleteEmployeeAllowance('1'));
      expect(store.getState().employeeAllowances.employeeAllowances).toEqual([]);
    });
  });
});
