import { configureStore } from '@reduxjs/toolkit';
import {
  EmployeeSlice,
  fetchEmployees,
  fetchEmployee,
  deleteEmployee,
  updateEmployeeStatus,
  clearEmployee,
} from '../EmployeeSlice';

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

const { apiDelete: apiDeleteMock, apiPut: apiPutMock } = require('@/services/api/api');

const createStore = () =>
  configureStore({ reducer: { employees: EmployeeSlice.reducer } });

describe('EmployeeSlice', () => {
  beforeEach(() => jest.clearAllMocks());

  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().employees;
    expect(state.employees).toEqual([]);
    expect(state.employee).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
    expect(state.uploadingPicture).toEqual({});
    expect(state.deletingPicture).toEqual({});
    expect(state.importingEmployee).toBe(false);
  });

  it('sets loading to true on fetchEmployees.pending', () => {
    const store = createStore();
    store.dispatch(fetchEmployees.pending('requestId', undefined));
    const state = store.getState().employees;
    expect(state.loading).toBe(true);
    expect(state.error).toBeNull();
  });

  it('populates employees on fetchEmployees.fulfilled', () => {
    const store = createStore();
    const mockEmployees = [{ employee_id: '1', firstname: 'John' }] as any;
    store.dispatch(fetchEmployees.fulfilled(mockEmployees, 'requestId', undefined));
    const state = store.getState().employees;
    expect(state.employees).toEqual(mockEmployees);
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

  it('sets loading false on fetchEmployees.rejected', () => {
    const store = createStore();
    store.dispatch(fetchEmployees.rejected(new Error('fail'), 'requestId', undefined));
    const state = store.getState().employees;
    expect(state.loading).toBe(false);
  });

  it('sets loading true on fetchEmployee.pending', () => {
    const store = createStore();
    store.dispatch(fetchEmployee.pending('requestId', 'emp-1'));
    const state = store.getState().employees;
    expect(state.loading).toBe(true);
  });

  it('sets employee on fetchEmployee.fulfilled', () => {
    const store = createStore();
    const mockEmployee = { employee_id: '1', firstname: 'Jane' } as any;
    store.dispatch(fetchEmployee.fulfilled(mockEmployee, 'requestId', 'emp-1'));
    const state = store.getState().employees;
    expect(state.employee).toEqual(mockEmployee);
    expect(state.loading).toBe(false);
  });

  it('clears employee via clearEmployee reducer', () => {
    const store = createStore();
    const mockEmployee = { employee_id: '1', firstname: 'Jane' } as any;
    store.dispatch(fetchEmployee.fulfilled(mockEmployee, 'requestId', 'emp-1'));
    store.dispatch(clearEmployee());
    const state = store.getState().employees;
    expect(state.employee).toBeNull();
  });

  describe('deleteEmployee', () => {
    it('removes employee from list on fulfilled', async () => {
      apiDeleteMock.mockResolvedValue({ data: { message: 'Employee deleted' } });
      const store = createStore();
      const mockEmployees = [
        { employee_id: '1', firstname: 'John' },
        { employee_id: '2', firstname: 'Jane' },
      ] as any;
      store.dispatch(fetchEmployees.fulfilled(mockEmployees, 'req', undefined));
      await store.dispatch(deleteEmployee('1'));
      const state = store.getState().employees;
      expect(state.employees).toHaveLength(1);
      expect(state.employees[0].employee_id).toBe('2');
    });

    it('does not modify employees list on rejected', async () => {
      apiDeleteMock.mockRejectedValue({ isAxiosError: true, response: { data: { message: 'Delete failed' } } });
      const store = createStore();
      const mockEmployees = [{ employee_id: '1', firstname: 'John' }] as any;
      store.dispatch(fetchEmployees.fulfilled(mockEmployees, 'req', undefined));
      await store.dispatch(deleteEmployee('1'));
      const state = store.getState().employees;
      expect(state.employees).toHaveLength(1);
    });
  });

  describe('updateEmployeeStatus', () => {
    it('updates both employees array and employee single object on fulfilled', async () => {
      apiPutMock.mockResolvedValue({ data: { status: 'inactive' } });
      const store = createStore();
      const emp = { employee_id: '1', firstname: 'John', status: 'active' } as any;
      store.dispatch(fetchEmployees.fulfilled([emp], 'req', undefined));
      store.dispatch(fetchEmployee.fulfilled(emp, 'req', '1'));
      await store.dispatch(updateEmployeeStatus({ employee_id: '1', status: 'inactive' }));
      const state = store.getState().employees;
      expect(state.employees[0].status).toBe('inactive');
      expect(state.employee?.status).toBe('inactive');
    });

    it('updates employees array even when employee single object is null', async () => {
      apiPutMock.mockResolvedValue({ data: { status: 'active' } });
      const store = createStore();
      const emp = { employee_id: '1', firstname: 'John', status: 'inactive' } as any;
      store.dispatch(fetchEmployees.fulfilled([emp], 'req', undefined));
      await store.dispatch(updateEmployeeStatus({ employee_id: '1', status: 'active' }));
      const state = store.getState().employees;
      expect(state.employees[0].status).toBe('active');
      expect(state.employee).toBeNull();
    });
  });

  describe('fetchEmployee rejected', () => {
    it('sets loading false on rejection', async () => {
      const store = createStore();
      store.dispatch(fetchEmployee.pending('req', 'emp-1'));
      expect(store.getState().employees.loading).toBe(true);
      store.dispatch(
        fetchEmployee.rejected(null as any, 'req', 'emp-1', { message: 'Test error' } as any),
      );
      expect(store.getState().employees.loading).toBe(false);
    });
  });
});
