import { configureStore } from '@reduxjs/toolkit';
import CheckInSlice, { clearCurrentCheckIn, fetchCheckIns, fetchCheckIn, fetchMyCheckIns, createCheckIn, updateCheckIn, deleteCheckIn } from '../CheckInSlice';

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: { checkIns: CheckInSlice.reducer } });

describe('CheckInSlice', () => {
  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().checkIns;
    expect(state.checkIns).toEqual([]);
    expect(state.myCheckIns).toEqual([]);
    expect(state.checkInHistory).toEqual([]);
    expect(state.overdueActions).toEqual([]);
    expect(state.currentCheckIn).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

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

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

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

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

  it('updates in list on updateCheckIn.fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: fetchCheckIns.fulfilled.type, payload: { data: [{ id: '1', status: 'scheduled' }], meta: {}, links: {} } });
    store.dispatch({ type: updateCheckIn.fulfilled.type, payload: { id: '1', status: 'updated' } });
    expect(store.getState().checkIns.checkIns[0].status).toBe('updated');
    expect(store.getState().checkIns.currentCheckIn?.id).toBe('1');
  });

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

  it('clears currentCheckIn via clearCurrentCheckIn', () => {
    const store = createStore();
    store.dispatch({ type: fetchCheckIn.fulfilled.type, payload: { id: '1' } });
    store.dispatch(clearCurrentCheckIn());
    expect(store.getState().checkIns.currentCheckIn).toBeNull();
  });
});
