import { configureStore } from '@reduxjs/toolkit';
import EmployeeTaxReliefsSlice, {
  fetchEmployeeTaxReliefs,
  createEmployeeTaxRelief,
  updateEmployeeTaxRelief,
  deleteEmployeeTaxRelief,
  clearEmployeeTaxReliefs,
} from '../EmployeeTaxReliefsSlice';

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: { employeeTaxReliefs: EmployeeTaxReliefsSlice.reducer } });

const mockRelief = { id: '1', employee_id: 'e1', tax_relief_category_id: 'trc1', amount: 1200 };

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

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

  describe('fetchEmployeeTaxReliefs', () => {
    it('populates reliefs on fulfilled', async () => {
      apiGet.mockResolvedValue({ data: [mockRelief] });
      const store = createTestStore();
      await store.dispatch(fetchEmployeeTaxReliefs());
      expect(store.getState().employeeTaxReliefs.employeeTaxReliefs).toEqual([mockRelief]);
    });
  });

  describe('createEmployeeTaxRelief', () => {
    it('appends new relief on fulfilled', async () => {
      apiPost.mockResolvedValue({ data: { data: mockRelief } });
      const store = createTestStore();
      await store.dispatch(createEmployeeTaxRelief({ employee_id: 'e1' } as any));
      expect(store.getState().employeeTaxReliefs.employeeTaxReliefs).toContainEqual(mockRelief);
    });
  });

  describe('updateEmployeeTaxRelief', () => {
    it('updates relief in list on fulfilled', async () => {
      const updated = { ...mockRelief, amount: 1500 };
      apiPut.mockResolvedValue({ data: { data: updated } });
      const store = createTestStore();
      store.dispatch({ type: fetchEmployeeTaxReliefs.fulfilled.type, payload: [mockRelief] });
      await store.dispatch(updateEmployeeTaxRelief({ id: '1', amount: 1500 } as any));
      expect(store.getState().employeeTaxReliefs.employeeTaxReliefs[0].amount).toBe(1500);
    });
  });

  describe('deleteEmployeeTaxRelief', () => {
    it('removes relief by id on fulfilled', async () => {
      apiDelete.mockResolvedValue({});
      const store = createTestStore();
      store.dispatch({ type: fetchEmployeeTaxReliefs.fulfilled.type, payload: [mockRelief] });
      await store.dispatch(deleteEmployeeTaxRelief('1'));
      expect(store.getState().employeeTaxReliefs.employeeTaxReliefs).toEqual([]);
    });
  });

  describe('clearEmployeeTaxReliefs', () => {
    it('clears all reliefs', () => {
      const store = createTestStore();
      store.dispatch({ type: fetchEmployeeTaxReliefs.fulfilled.type, payload: [mockRelief] });
      store.dispatch(clearEmployeeTaxReliefs());
      expect(store.getState().employeeTaxReliefs.employeeTaxReliefs).toEqual([]);
    });
  });
});
