import { configureStore } from '@reduxjs/toolkit';
import GoalSlice, { clearCurrentGoal, fetchGoals, fetchMyGoals, fetchTeamGoals, fetchGoal, createGoal, deleteGoal, submitGoalUpdate } from '../GoalSlice';

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: { goals: GoalSlice.reducer } });

describe('GoalSlice', () => {
  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().goals;
    expect(state.goals).toEqual([]);
    expect(state.myGoals).toEqual([]);
    expect(state.teamGoals).toEqual([]);
    expect(state.currentGoal).toBeNull();
    expect(state.goalUpdates).toEqual([]);
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

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

  it('populates goals on fetchGoals.fulfilled', () => {
    const store = createStore();
    const payload = { data: [{ id: '1' }], meta: { current_page: 1 }, links: {} };
    store.dispatch({ type: fetchGoals.fulfilled.type, payload });
    expect(store.getState().goals.goals).toEqual(payload.data);
    expect(store.getState().goals.loading).toBe(false);
  });

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

  it('populates teamGoals on fetchTeamGoals.fulfilled', () => {
    const store = createStore();
    const payload = { data: [{ id: '2' }], meta: { current_page: 1 } };
    store.dispatch({ type: fetchTeamGoals.fulfilled.type, payload });
    expect(store.getState().goals.teamGoals).toEqual(payload.data);
  });

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

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

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

  it('prepends goalUpdate on submitGoalUpdate.fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: submitGoalUpdate.fulfilled.type, payload: { id: 'u1' } });
    expect(store.getState().goals.goalUpdates[0]).toEqual({ id: 'u1' });
  });

  it('clears currentGoal via clearCurrentGoal', () => {
    const store = createStore();
    store.dispatch({ type: fetchGoal.fulfilled.type, payload: { id: '1' } });
    store.dispatch(clearCurrentGoal());
    expect(store.getState().goals.currentGoal).toBeNull();
  });
});
