import { configureStore } from '@reduxjs/toolkit';
import TrainingEvaluationsSlice from '../TrainingEvaluationsSlice';

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', () => ({ default: { handleServerError: jest.fn((e) => e?.message || 'Error') } }));

const createStore = () => configureStore({ reducer: { tev: TrainingEvaluationsSlice.reducer } });

describe('TrainingEvaluationsSlice', () => {
  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().tev;
    expect(state.evaluations).toEqual([]);
    expect(state.pendingEvaluations).toEqual([]);
    expect(state.sessionReport).toBeNull();
    expect(state.programSummary).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

  it('sets loading on fetch pending', () => {
    const store = createStore();
    store.dispatch({ type: 'trainingEvaluations/fetch/pending' });
    expect(store.getState().tev.loading).toBe(true);
  });

  it('populates evaluations on fetch fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'trainingEvaluations/fetch/fulfilled', payload: [{ id: '1' }] });
    expect(store.getState().tev.evaluations).toEqual([{ id: '1' }]);
  });

  it('populates pendingEvaluations on fetchPending fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'trainingEvaluations/fetchPending/fulfilled', payload: [{ id: 'p1' }] });
    expect(store.getState().tev.pendingEvaluations).toEqual([{ id: 'p1' }]);
  });

  it('populates sessionReport on fetchSessionReport fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'trainingEvaluations/fetchSessionReport/fulfilled', payload: { avgScore: 4.5 } });
    expect(store.getState().tev.sessionReport).toEqual({ avgScore: 4.5 });
  });

  it('populates programSummary on fetchProgramSummary fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'trainingEvaluations/fetchProgramSummary/fulfilled', payload: { total: 10 } });
    expect(store.getState().tev.programSummary).toEqual({ total: 10 });
  });

  it('prepends on create fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'trainingEvaluations/fetch/fulfilled', payload: [{ id: '1' }] });
    store.dispatch({ type: 'trainingEvaluations/create/fulfilled', payload: { id: '2' } });
    expect(store.getState().tev.evaluations[0]).toEqual({ id: '2' });
  });

  it('updates in-place on update fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'trainingEvaluations/fetch/fulfilled', payload: [{ id: '1', rating: 3 }] });
    store.dispatch({ type: 'trainingEvaluations/update/fulfilled', payload: { id: '1', rating: 5 } });
    expect(store.getState().tev.evaluations[0].rating).toBe(5);
  });

  it('removes on delete fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'trainingEvaluations/fetch/fulfilled', payload: [{ id: '1' }] });
    store.dispatch({ type: 'trainingEvaluations/delete/fulfilled', payload: { id: '1', message: 'Deleted' } });
    expect(store.getState().tev.evaluations).toEqual([]);
  });
});
