import { configureStore } from '@reduxjs/toolkit';
import { DependantSlice, fetchDependants, createDependant, fetchDependantRelationshipTypes } from '../DependantSlice';

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: { dependants: DependantSlice.reducer } });

describe('DependantSlice', () => {
  it('has correct initial state', () => {
    const state = createStore().getState().dependants;
    expect(state.dependants).toEqual([]);
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
    expect(state.relationshipTypes).toBeUndefined();
    expect(state.loadingRelationshipTypes).toBe(false);
    expect(state.deleting).toBe(false);
    expect(state.uploadingDocument).toBe(false);
  });

  it('sets loading on fetchDependants.pending', () => {
    const store = createStore();
    store.dispatch(fetchDependants.pending('req', 'emp-1'));
    expect(store.getState().dependants.loading).toBe(true);
  });

  it('populates dependants on fetchDependants.fulfilled', () => {
    const store = createStore();
    const data = [{ id: '1', name: 'Dep' }] as any;
    store.dispatch(fetchDependants.fulfilled(data, 'req', 'emp-1'));
    expect(store.getState().dependants.dependants).toEqual(data);
    expect(store.getState().dependants.loading).toBe(false);
  });

  it('sets loadingRelationshipTypes on fetchDependantRelationshipTypes.pending', () => {
    const store = createStore();
    store.dispatch(fetchDependantRelationshipTypes.pending('req'));
    expect(store.getState().dependants.loadingRelationshipTypes).toBe(true);
  });
});
