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

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: { ec: EmployeeCertificationsSlice.reducer } });

describe('EmployeeCertificationsSlice', () => {
  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().ec;
    expect(state.employeeCertifications).toEqual([]);
    expect(state.myCertifications).toEqual([]);
    expect(state.meta).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

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

  it('populates with paginated data on fetch fulfilled', () => {
    const store = createStore();
    const meta = { current_page: 1, last_page: 1, per_page: 15, total: 1 };
    store.dispatch({ type: 'employeeCertifications/fetch/fulfilled', payload: { data: [{ id: '1' }], meta } });
    expect(store.getState().ec.employeeCertifications).toEqual([{ id: '1' }]);
    expect(store.getState().ec.meta).toEqual(meta);
  });

  it('populates with plain array on fetch fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'employeeCertifications/fetch/fulfilled', payload: [{ id: '1' }] });
    expect(store.getState().ec.employeeCertifications).toEqual([{ id: '1' }]);
    expect(store.getState().ec.meta).toBeNull();
  });

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

  it('updates in-place on update fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'employeeCertifications/fetch/fulfilled', payload: [{ id: '1', status: 'active' }] });
    store.dispatch({ type: 'employeeCertifications/update/fulfilled', payload: { id: '1', status: 'expired' } });
    expect(store.getState().ec.employeeCertifications[0].status).toBe('expired');
  });

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

  it('populates myCertifications on fetchMyCertifications fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'employeeCertifications/fetchMyCertifications/fulfilled', payload: [{ id: 'm1' }] });
    expect(store.getState().ec.myCertifications).toEqual([{ id: 'm1' }]);
  });
});
