import { configureStore } from '@reduxjs/toolkit';
import TrainingReportsSlice, { clearTrainingReport, fetchTrainingSummaryReport, fetchEmployeeTrainingRecord, fetchComplianceReport } from '../TrainingReportsSlice';

jest.mock('@/services/api/api', () => ({ apiGet: jest.fn() }));
jest.mock('antd', () => ({ message: { success: jest.fn(), error: jest.fn(), warning: jest.fn() } }));
jest.mock('@/utilities/Helpers', () => ({
  __esModule: true, default: { handleServerError: jest.fn((e) => e?.message || 'Error') } }));

const createStore = () => configureStore({ reducer: { tr: TrainingReportsSlice.reducer } });

describe('TrainingReportsSlice', () => {
  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().tr;
    expect(state.summaryReport).toBeNull();
    expect(state.employeeRecord).toBeNull();
    expect(state.complianceReport).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

  it('sets summaryReport on fetchTrainingSummaryReport.fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: fetchTrainingSummaryReport.fulfilled.type, payload: { total: 50 } });
    expect(store.getState().tr.summaryReport).toEqual({ total: 50 });
  });

  it('sets employeeRecord on fetchEmployeeTrainingRecord.fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: fetchEmployeeTrainingRecord.fulfilled.type, payload: { hours: 40 } });
    expect(store.getState().tr.employeeRecord).toEqual({ hours: 40 });
  });

  it('sets complianceReport on fetchComplianceReport.fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: fetchComplianceReport.fulfilled.type, payload: { rate: 95 } });
    expect(store.getState().tr.complianceReport).toEqual({ rate: 95 });
  });

  it('clears all via clearTrainingReport', () => {
    const store = createStore();
    store.dispatch({ type: fetchTrainingSummaryReport.fulfilled.type, payload: { total: 50 } });
    store.dispatch(clearTrainingReport());
    expect(store.getState().tr.summaryReport).toBeNull();
    expect(store.getState().tr.error).toBeNull();
  });
});
