import { configureStore } from '@reduxjs/toolkit';
import PipSlice, { clearCurrentPip, fetchPips, fetchPip, createPip, activatePip, fetchPipProgress, deletePip } from '../PipSlice';

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

const createStore = () => configureStore({ reducer: { pips: PipSlice.reducer } });

describe('PipSlice', () => {
  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().pips;
    expect(state.pips).toEqual([]);
    expect(state.myPips).toEqual([]);
    expect(state.currentPip).toBeNull();
    expect(state.pipProgress).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

  it('populates on fetchPips.fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: fetchPips.fulfilled.type, payload: { data: [{ id: '1' }], meta: {}, links: {} } });
    expect(store.getState().pips.pips).toEqual([{ id: '1' }]);
  });

  it('sets currentPip on fetchPip.fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: fetchPip.fulfilled.type, payload: { id: '1' } });
    expect(store.getState().pips.currentPip).toEqual({ id: '1' });
  });

  it('prepends on createPip.fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: createPip.fulfilled.type, payload: { id: '1' } });
    expect(store.getState().pips.pips[0]).toEqual({ id: '1' });
  });

  it('updates currentPip on activatePip.fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: fetchPips.fulfilled.type, payload: { data: [{ id: '1', status: 'draft' }], meta: {}, links: {} } });
    store.dispatch({ type: activatePip.fulfilled.type, payload: { id: '1', status: 'active' } });
    expect(store.getState().pips.currentPip?.status).toBe('active');
    expect(store.getState().pips.pips[0].status).toBe('active');
  });

  it('sets pipProgress on fetchPipProgress.fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: fetchPipProgress.fulfilled.type, payload: { overall: 50 } });
    expect(store.getState().pips.pipProgress).toEqual({ overall: 50 });
  });

  it('removes on deletePip.fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: fetchPips.fulfilled.type, payload: { data: [{ id: '1' }], meta: {}, links: {} } });
    store.dispatch({ type: deletePip.fulfilled.type, payload: { id: '1', message: 'Deleted' } });
    expect(store.getState().pips.pips).toHaveLength(0);
  });

  it('clears via clearCurrentPip', () => {
    const store = createStore();
    store.dispatch({ type: fetchPip.fulfilled.type, payload: { id: '1' } });
    store.dispatch({ type: fetchPipProgress.fulfilled.type, payload: { overall: 50 } });
    store.dispatch(clearCurrentPip());
    expect(store.getState().pips.currentPip).toBeNull();
    expect(store.getState().pips.pipProgress).toBeNull();
  });
});
