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

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: { cert: CertificationsSlice.reducer } });

describe('CertificationsSlice', () => {
  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().cert;
    expect(state.certifications).toEqual([]);
    expect(state.expiringCertifications).toEqual([]);
    expect(state.complianceReport).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

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

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

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

  it('updates in-place on update fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'certifications/fetch/fulfilled', payload: [{ id: '1', name: 'Old' }] });
    store.dispatch({ type: 'certifications/update/fulfilled', payload: { id: '1', name: 'New' } });
    expect(store.getState().cert.certifications[0].name).toBe('New');
  });

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

  it('populates expiringCertifications on fetchExpiring fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'certifications/fetchExpiring/fulfilled', payload: [{ id: 'e1' }] });
    expect(store.getState().cert.expiringCertifications).toEqual([{ id: 'e1' }]);
  });

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