/*
 * File: aiPredictionSlice.test.ts
 * Description: Unit tests for AI Prediction Redux slice
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */

import aiPredictionReducer, {
  setSearchQuery,
  setUrgencyFilter,
  setSeverityFilter,
  setCategoryFilter,
  clearAllFilters,
  toggleShowFilters,
  clearError,
} from '../redux/aiPredictionSlice';
import type { AIPredictionState } from '../types';

// ============================================================================
// MOCK DATA
// ============================================================================

const initialState: AIPredictionState = {
  predictionCases: [],
  currentCase: null,
  isLoading: false,
  isRefreshing: false,
  isLoadingCaseDetails: false,
  error: null,
  searchQuery: '',
  selectedUrgencyFilter: 'all',
  selectedSeverityFilter: 'all',
  selectedCategoryFilter: 'all',
  sortBy: 'date',
  sortOrder: 'desc',
  currentPage: 1,
  itemsPerPage: 20,
  totalItems: 0,
  lastUpdated: null,
  cacheExpiry: null,
  showFilters: false,
  selectedCaseIds: [],
};

// ============================================================================
// UNIT TESTS
// ============================================================================

describe('AI Prediction Slice', () => {
  // ============================================================================
  // INITIAL STATE TESTS
  // ============================================================================

  it('should return the initial state', () => {
    const result = aiPredictionReducer(undefined, { type: 'unknown' });
    expect(result).toEqual(initialState);
  });

  // ============================================================================
  // SEARCH TESTS
  // ============================================================================

  describe('Search functionality', () => {
    it('should handle setSearchQuery', () => {
      const searchQuery = 'test search';
      const action = setSearchQuery(searchQuery);
      const result = aiPredictionReducer(initialState, action);

      expect(result.searchQuery).toBe(searchQuery);
      expect(result.currentPage).toBe(1); // Should reset to first page
    });

    it('should handle empty search query', () => {
      const state = { ...initialState, searchQuery: 'existing search' };
      const action = setSearchQuery('');
      const result = aiPredictionReducer(state, action);

      expect(result.searchQuery).toBe('');
      expect(result.currentPage).toBe(1);
    });
  });

  // ============================================================================
  // FILTER TESTS
  // ============================================================================

  describe('Filter functionality', () => {
    it('should handle setUrgencyFilter', () => {
      const filter = 'urgent';
      const action = setUrgencyFilter(filter);
      const result = aiPredictionReducer(initialState, action);

      expect(result.selectedUrgencyFilter).toBe(filter);
      expect(result.currentPage).toBe(1); // Should reset to first page
    });

    it('should handle setSeverityFilter', () => {
      const filter = 'high';
      const action = setSeverityFilter(filter);
      const result = aiPredictionReducer(initialState, action);

      expect(result.selectedSeverityFilter).toBe(filter);
      expect(result.currentPage).toBe(1);
    });

    it('should handle setCategoryFilter', () => {
      const filter = 'critical';
      const action = setCategoryFilter(filter);
      const result = aiPredictionReducer(initialState, action);

      expect(result.selectedCategoryFilter).toBe(filter);
      expect(result.currentPage).toBe(1);
    });

    it('should handle clearAllFilters', () => {
      const state: AIPredictionState = {
        ...initialState,
        searchQuery: 'test',
        selectedUrgencyFilter: 'urgent',
        selectedSeverityFilter: 'high',
        selectedCategoryFilter: 'critical',
        currentPage: 3,
      };

      const action = clearAllFilters();
      const result = aiPredictionReducer(state, action);

      expect(result.searchQuery).toBe('');
      expect(result.selectedUrgencyFilter).toBe('all');
      expect(result.selectedSeverityFilter).toBe('all');
      expect(result.selectedCategoryFilter).toBe('all');
      expect(result.currentPage).toBe(1);
    });
  });

  // ============================================================================
  // UI STATE TESTS
  // ============================================================================

  describe('UI state functionality', () => {
    it('should handle toggleShowFilters', () => {
      const action = toggleShowFilters();
      
      // Toggle from false to true
      const result1 = aiPredictionReducer(initialState, action);
      expect(result1.showFilters).toBe(true);
      
      // Toggle from true to false
      const result2 = aiPredictionReducer(result1, action);
      expect(result2.showFilters).toBe(false);
    });

    it('should handle clearError', () => {
      const state = { ...initialState, error: 'Test error' };
      const action = clearError();
      const result = aiPredictionReducer(state, action);

      expect(result.error).toBe(null);
    });
  });

  // ============================================================================
  // ASYNC ACTION TESTS
  // ============================================================================

  describe('Async actions', () => {
    it('should handle fetchAIPredictions.pending', () => {
      const action = { type: 'aiPrediction/fetchAIPredictions/pending' };
      const result = aiPredictionReducer(initialState, action);

      expect(result.isLoading).toBe(true);
      expect(result.error).toBe(null);
    });

    it('should handle fetchAIPredictions.fulfilled', () => {
      const mockCases = [
        {
          patid: 'test-001',
          hospital_id: 'hospital-001',
          prediction: {
            label: 'test finding',
            finding_type: 'pathology' as const,
            clinical_urgency: 'urgent' as const,
            confidence_score: 0.95,
            finding_category: 'abnormal' as const,
            primary_severity: 'high' as const,
            anatomical_location: 'brain',
          },
        },
      ];

      const action = {
        type: 'aiPrediction/fetchAIPredictions/fulfilled',
        payload: {
          cases: mockCases,
          total: 1,
          page: 1,
          limit: 20,
        },
      };

      const result = aiPredictionReducer(initialState, action);

      expect(result.isLoading).toBe(false);
      expect(result.predictionCases).toEqual(mockCases);
      expect(result.totalItems).toBe(1);
      expect(result.error).toBe(null);
      expect(result.lastUpdated).toBeInstanceOf(Date);
      expect(result.cacheExpiry).toBeInstanceOf(Date);
    });

    it('should handle fetchAIPredictions.rejected', () => {
      const errorMessage = 'Failed to fetch predictions';
      const action = {
        type: 'aiPrediction/fetchAIPredictions/rejected',
        payload: errorMessage,
      };

      const result = aiPredictionReducer(initialState, action);

      expect(result.isLoading).toBe(false);
      expect(result.error).toBe(errorMessage);
    });
  });
});

/*
 * End of File: aiPredictionSlice.test.ts
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */
