import { configureStore } from '@reduxjs/toolkit';
import { DataMigrationSlice, uploadDump, fetchImportStatus, fetchAnalysis, startMigration, fetchStatus, fetchReport, cleanup, setUploadProgress, resetState } from '../DataMigrationSlice';

jest.mock('@/services/api/dataMigration', () => ({
  uploadLegacyDump: jest.fn(),
  getImportStatus: jest.fn(),
  getAnalysis: jest.fn(),
  startMigration: jest.fn(),
  getMigrationStatus: jest.fn(),
  getReconciliationReport: jest.fn(),
  cleanupMigration: 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: { dataMigration: DataMigrationSlice.reducer } });

describe('DataMigrationSlice', () => {
  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().dataMigration;
    expect(state.uploading).toBe(false);
    expect(state.uploadProgress).toBe(0);
    expect(state.importStatus).toBeNull();
    expect(state.analysis).toBeNull();
    expect(state.loadingAnalysis).toBe(false);
    expect(state.migrationStatus).toBeNull();
    expect(state.loadingStatus).toBe(false);
    expect(state.starting).toBe(false);
    expect(state.report).toBeNull();
    expect(state.loadingReport).toBe(false);
    expect(state.cleaningUp).toBe(false);
    expect(state.error).toBeNull();
  });

  describe('reducers', () => {
    it('setUploadProgress updates progress', () => {
      const store = createStore();
      store.dispatch(setUploadProgress(50));
      expect(store.getState().dataMigration.uploadProgress).toBe(50);
    });

    it('resetState returns initial state', () => {
      const store = createStore();
      store.dispatch(setUploadProgress(75));
      store.dispatch(resetState());
      expect(store.getState().dataMigration.uploadProgress).toBe(0);
    });
  });

  describe('uploadDump', () => {
    it('sets uploading on pending', () => {
      const store = createStore();
      store.dispatch({ type: uploadDump.pending.type });
      expect(store.getState().dataMigration.uploading).toBe(true);
      expect(store.getState().dataMigration.uploadProgress).toBe(0);
    });

    it('sets importStatus on fulfilled', () => {
      const store = createStore();
      store.dispatch({ type: uploadDump.fulfilled.type });
      expect(store.getState().dataMigration.uploading).toBe(false);
      expect(store.getState().dataMigration.uploadProgress).toBe(100);
      expect(store.getState().dataMigration.importStatus?.status).toBe('queued');
    });
  });

  describe('fetchImportStatus', () => {
    it('sets importStatus on fulfilled', () => {
      const store = createStore();
      const payload = { status: 'ready', message: 'Done', analysis: { summary: {} } };
      store.dispatch({ type: fetchImportStatus.fulfilled.type, payload });
      expect(store.getState().dataMigration.importStatus).toEqual(payload);
      expect(store.getState().dataMigration.analysis).toEqual(payload.analysis);
    });
  });

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

    it('populates analysis on fulfilled', () => {
      const store = createStore();
      const analysis = { summary: {}, data_quality: {} };
      store.dispatch({ type: fetchAnalysis.fulfilled.type, payload: analysis });
      expect(store.getState().dataMigration.analysis).toEqual(analysis);
      expect(store.getState().dataMigration.loadingAnalysis).toBe(false);
    });
  });

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

    it('resets starting on fulfilled', () => {
      const store = createStore();
      store.dispatch({ type: startMigration.fulfilled.type });
      expect(store.getState().dataMigration.starting).toBe(false);
    });
  });

  describe('fetchStatus', () => {
    it('sets migrationStatus on fulfilled', () => {
      const store = createStore();
      const status = { phase: 1, phaseLabel: 'Employees', progress: 50 };
      store.dispatch({ type: fetchStatus.fulfilled.type, payload: status });
      expect(store.getState().dataMigration.migrationStatus).toEqual(status);
    });
  });

  describe('fetchReport', () => {
    it('sets report on fulfilled', () => {
      const store = createStore();
      const report = { comparisons: [], payroll_reconciliation: [] };
      store.dispatch({ type: fetchReport.fulfilled.type, payload: report });
      expect(store.getState().dataMigration.report).toEqual(report);
    });
  });

  describe('cleanup', () => {
    it('resets to initial state on fulfilled', () => {
      const store = createStore();
      store.dispatch(setUploadProgress(80));
      store.dispatch({ type: cleanup.fulfilled.type });
      expect(store.getState().dataMigration.uploadProgress).toBe(0);
      expect(store.getState().dataMigration.cleaningUp).toBe(false);
    });
  });
});
