import { configureStore } from '@reduxjs/toolkit';
import { NotificationSlice, fetchNotifications, fetchUnreadCount, markAsRead, markAllAsRead, fetchNotificationPreferences, updateNotificationPreferences, clearNotifications } from '../NotificationSlice';

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: { notifications: NotificationSlice.reducer } });

describe('NotificationSlice', () => {
  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().notifications;
    expect(state.notifications).toBeNull();
    expect(state.unreadCount).toBe(0);
    expect(state.preferences).toEqual([]);
    expect(state.loading).toBe(false);
    expect(state.preferencesLoading).toBe(false);
    expect(state.error).toBeNull();
  });

  describe('reducers', () => {
    it('clearNotifications resets notifications', () => {
      const store = createStore();
      store.dispatch({ type: fetchNotifications.fulfilled.type, payload: { data: [], meta: {} } });
      store.dispatch(clearNotifications());
      expect(store.getState().notifications.notifications).toBeNull();
    });
  });

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

    it('populates notifications on fulfilled', () => {
      const store = createStore();
      const payload = { data: [{ id: 'n1', read_at: null }], meta: {} };
      store.dispatch({ type: fetchNotifications.fulfilled.type, payload });
      expect(store.getState().notifications.notifications).toEqual(payload);
      expect(store.getState().notifications.loading).toBe(false);
    });
  });

  describe('fetchUnreadCount', () => {
    it('sets unreadCount on fulfilled', () => {
      const store = createStore();
      store.dispatch({ type: fetchUnreadCount.fulfilled.type, payload: 5 });
      expect(store.getState().notifications.unreadCount).toBe(5);
    });
  });

  describe('markAsRead', () => {
    it('marks notification as read and decrements count', () => {
      const store = createStore();
      const notifs = { data: [{ id: 'n1', read_at: null }, { id: 'n2', read_at: null }], meta: {} };
      store.dispatch({ type: fetchNotifications.fulfilled.type, payload: notifs });
      store.dispatch({ type: fetchUnreadCount.fulfilled.type, payload: 2 });
      store.dispatch({ type: markAsRead.fulfilled.type, payload: 'n1' });
      expect(store.getState().notifications.notifications?.data[0].read_at).toBeTruthy();
      expect(store.getState().notifications.unreadCount).toBe(1);
    });
  });

  describe('markAllAsRead', () => {
    it('marks all as read and resets count', () => {
      const store = createStore();
      const notifs = { data: [{ id: 'n1', read_at: null }], meta: {} };
      store.dispatch({ type: fetchNotifications.fulfilled.type, payload: notifs });
      store.dispatch({ type: fetchUnreadCount.fulfilled.type, payload: 1 });
      store.dispatch({ type: markAllAsRead.fulfilled.type });
      expect(store.getState().notifications.unreadCount).toBe(0);
      expect(store.getState().notifications.notifications?.data[0].read_at).toBeTruthy();
    });
  });

  describe('fetchNotificationPreferences', () => {
    it('populates preferences on fulfilled', () => {
      const store = createStore();
      const prefs = [{ id: 'p1', channel: 'email', enabled: true }];
      store.dispatch({ type: fetchNotificationPreferences.fulfilled.type, payload: prefs });
      expect(store.getState().notifications.preferences).toEqual(prefs);
    });
  });

  describe('updateNotificationPreferences', () => {
    it('updates preferences on fulfilled', () => {
      const store = createStore();
      const prefs = [{ id: 'p1', channel: 'email', enabled: false }];
      store.dispatch({ type: updateNotificationPreferences.fulfilled.type, payload: prefs });
      expect(store.getState().notifications.preferences).toEqual(prefs);
    });
  });
});
