import { configureStore } from '@reduxjs/toolkit';
import EmployeeDeductionsSlice, {
  fetchEmployeeDeductions,
  fetchDeductionMatrix,
  createEmployeeDeduction,
  updateEmployeeDeduction,
  deleteEmployeeDeduction,
} from '../EmployeeDeductionsSlice';

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: { employeeDeductions: EmployeeDeductionsSlice.reducer } });

const mockDeduction = { id: '1', employee_id: 'e1', deduction_type_id: 'dt1', amount: 300 };

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

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

  describe('fetchEmployeeDeductions', () => {
    it('populates deductions on fulfilled', async () => {
      apiGet.mockResolvedValue({ data: [mockDeduction] });
      const store = createTestStore();
      await store.dispatch(fetchEmployeeDeductions());
      expect(store.getState().employeeDeductions.employeeDeductions).toEqual([mockDeduction]);
    });
  });

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

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

  describe('updateEmployeeDeduction', () => {
    it('updates deduction in list on fulfilled', async () => {
      const updated = { ...mockDeduction, amount: 500 };
      apiPut.mockResolvedValue({ data: { data: updated } });
      const store = createTestStore();
      store.dispatch({ type: fetchEmployeeDeductions.fulfilled.type, payload: [mockDeduction] });
      await store.dispatch(updateEmployeeDeduction({ id: '1', amount: 500 } as any));
      expect(store.getState().employeeDeductions.employeeDeductions[0].amount).toBe(500);
    });
  });

  describe('deleteEmployeeDeduction', () => {
    it('removes deduction from list on fulfilled', async () => {
      apiDelete.mockResolvedValue({ data: { id: '1', message: 'Deleted' } });
      const store = createTestStore();
      store.dispatch({ type: fetchEmployeeDeductions.fulfilled.type, payload: [mockDeduction] });
      await store.dispatch(deleteEmployeeDeduction('1'));
      expect(store.getState().employeeDeductions.employeeDeductions).toEqual([]);
    });
  });
});
