import { configureStore } from '@reduxjs/toolkit';
import AppraisalCycleSlice, { clearCurrentCycle, fetchAppraisalCycles, activateCycle, deleteAppraisalCycle } from '../AppraisalCycleSlice';

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: { appraisalCycles: AppraisalCycleSlice.reducer } });

describe('AppraisalCycleSlice', () => {
  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().appraisalCycles;
    expect(state.appraisalCycles).toEqual([]);
    expect(state.currentCycle).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

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

  it('populates cycles on fetchAppraisalCycles.fulfilled', () => {
    const store = createStore();
    const payload = {
      data: [{ id: '1', name: 'Q1' }],
      meta: { current_page: 1 },
      links: { first: '/1' },
    };
    store.dispatch({ type: fetchAppraisalCycles.fulfilled.type, payload });
    const state = store.getState().appraisalCycles;
    expect(state.appraisalCycles).toEqual(payload.data);
    expect(state.loading).toBe(false);
  });

  it('sets currentCycle on activateCycle.fulfilled', () => {
    const store = createStore();
    const cycle = { id: '1', name: 'Active', status: 'active' };
    store.dispatch({
      type: fetchAppraisalCycles.fulfilled.type,
      payload: { data: [{ id: '1', name: 'Draft', status: 'draft' }], meta: undefined, links: undefined },
    });
    store.dispatch({ type: activateCycle.fulfilled.type, payload: cycle });
    const state = store.getState().appraisalCycles;
    expect(state.currentCycle).toEqual(cycle);
    expect(state.appraisalCycles[0]).toEqual(cycle);
  });

  it('clears currentCycle via clearCurrentCycle', () => {
    const store = createStore();
    store.dispatch({ type: activateCycle.fulfilled.type, payload: { id: '1' } });
    store.dispatch(clearCurrentCycle());
    expect(store.getState().appraisalCycles.currentCycle).toBeNull();
  });

  it('removes item on deleteAppraisalCycle.fulfilled', () => {
    const store = createStore();
    store.dispatch({
      type: fetchAppraisalCycles.fulfilled.type,
      payload: { data: [{ id: '1' }, { id: '2' }], meta: undefined, links: undefined },
    });
    store.dispatch({
      type: deleteAppraisalCycle.fulfilled.type,
      payload: { id: '1', message: 'Deleted' },
    });
    expect(store.getState().appraisalCycles.appraisalCycles).toHaveLength(1);
  });
});
