/*
 * File: aiDashboardSelectors.ts
 * Description: Selectors for AI dashboard state management
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */

import { createSelector } from '@reduxjs/toolkit';
import { RootState } from '../../../store';

// ============================================================================
// BASE SELECTORS
// ============================================================================

/**
 * Select AI Dashboard State
 * 
 * Purpose: Get the entire AI dashboard state from root state
 */
const selectAIDashboardState = (state: RootState) => state.aiDashboard;

/**
 * Select AI Dashboard Data
 * 
 * Purpose: Get the AI dashboard data from state
 */
export const selectAIDashboardData = createSelector(
  [selectAIDashboardState],
  (aiDashboard) => aiDashboard.dashboardData
);

/**
 * Select AI Dashboard Statistics
 * 
 * Purpose: Get the AI dashboard statistics data
 */
export const selectAIDashboardStats = createSelector(
  [selectAIDashboardData],
  (dashboardData) => dashboardData?.data
);

/**
 * Select AI Dashboard Summary
 * 
 * Purpose: Get the AI dashboard summary data
 */
export const selectAIDashboardSummary = createSelector(
  [selectAIDashboardData],
  (dashboardData) => dashboardData?.summary
);

// ============================================================================
// LOADING STATE SELECTORS
// ============================================================================

/**
 * Select AI Dashboard Loading State
 * 
 * Purpose: Get the loading state for AI dashboard
 */
export const selectAIDashboardLoading = createSelector(
  [selectAIDashboardState],
  (aiDashboard) => aiDashboard.isLoading
);

/**
 * Select AI Dashboard Refreshing State
 * 
 * Purpose: Get the refreshing state for AI dashboard
 */
export const selectAIDashboardRefreshing = createSelector(
  [selectAIDashboardState],
  (aiDashboard) => aiDashboard.isRefreshing
);

// ============================================================================
// ERROR STATE SELECTORS
// ============================================================================

/**
 * Select AI Dashboard Error
 * 
 * Purpose: Get the error state for AI dashboard
 */
export const selectAIDashboardError = createSelector(
  [selectAIDashboardState],
  (aiDashboard) => aiDashboard.error
);

// ============================================================================
// FILTER STATE SELECTORS
// ============================================================================

/**
 * Select Selected Time Range
 * 
 * Purpose: Get the currently selected time range filter
 */
export const selectSelectedTimeRange = createSelector(
  [selectAIDashboardState],
  (aiDashboard) => aiDashboard.selectedTimeRange
);

/**
 * Select Selected Hospital
 * 
 * Purpose: Get the currently selected hospital filter
 */
export const selectSelectedHospital = createSelector(
  [selectAIDashboardState],
  (aiDashboard) => aiDashboard.selectedHospital
);

/**
 * Select Selected Department
 * 
 * Purpose: Get the currently selected department filter
 */
export const selectSelectedDepartment = createSelector(
  [selectAIDashboardState],
  (aiDashboard) => aiDashboard.selectedDepartment
);

// ============================================================================
// DERIVED DATA SELECTORS
// ============================================================================

/**
 * Select Total Predictions Count
 * 
 * Purpose: Get the total number of AI predictions
 */
export const selectTotalPredictions = createSelector(
  [selectAIDashboardStats],
  (stats) => stats?.total_predictions || 0
);

/**
 * Select Total Patients Count
 * 
 * Purpose: Get the total number of unique patients
 */
export const selectTotalPatients = createSelector(
  [selectAIDashboardStats],
  (stats) => stats?.total_patients || 0
);

/**
 * Select Total Feedbacks Count
 * 
 * Purpose: Get the total number of feedbacks received
 */
export const selectTotalFeedbacks = createSelector(
  [selectAIDashboardStats],
  (stats) => stats?.total_feedbacks || 0
);

/**
 * Select Feedback Rate Percentage
 * 
 * Purpose: Get the feedback rate as a percentage
 */
export const selectFeedbackRatePercentage = createSelector(
  [selectAIDashboardStats],
  (stats) => stats?.feedback_rate_percentage || 0
);

/**
 * Select Average Confidence Score
 * 
 * Purpose: Get the average confidence score for AI predictions
 */
export const selectAverageConfidenceScore = createSelector(
  [selectAIDashboardStats],
  (stats) => stats?.average_confidence_score || 0
);

/**
 * Select Critical Case Percentage
 * 
 * Purpose: Get the percentage of critical cases
 */
export const selectCriticalCasePercentage = createSelector(
  [selectAIDashboardStats],
  (stats) => stats?.critical_case_percentage || 0
);

// ============================================================================
// CONFIDENCE SCORE SELECTORS
// ============================================================================

/**
 * Select Confidence Score Distribution
 * 
 * Purpose: Get the distribution of confidence scores
 */
export const selectConfidenceScores = createSelector(
  [selectAIDashboardStats],
  (stats) => stats?.confidence_scores || { high: 0, medium: 0, low: 0 }
);

/**
 * Select High Confidence Count
 * 
 * Purpose: Get the count of high confidence predictions
 */
export const selectHighConfidenceCount = createSelector(
  [selectConfidenceScores],
  (confidenceScores) => confidenceScores.high
);

/**
 * Select Medium Confidence Count
 * 
 * Purpose: Get the count of medium confidence predictions
 */
export const selectMediumConfidenceCount = createSelector(
  [selectConfidenceScores],
  (confidenceScores) => confidenceScores.medium
);

/**
 * Select Low Confidence Count
 * 
 * Purpose: Get the count of low confidence predictions
 */
export const selectLowConfidenceCount = createSelector(
  [selectConfidenceScores],
  (confidenceScores) => confidenceScores.low
);

// ============================================================================
// URGENCY LEVEL SELECTORS
// ============================================================================

/**
 * Select Urgency Level Distribution
 * 
 * Purpose: Get the distribution of urgency levels
 */
export const selectUrgencyLevels = createSelector(
  [selectAIDashboardStats],
  (stats) => stats?.urgency_levels || { critical: 0, urgent: 0, routine: 0 }
);

/**
 * Select Critical Urgency Count
 * 
 * Purpose: Get the count of critical urgency cases
 */
export const selectCriticalUrgencyCount = createSelector(
  [selectUrgencyLevels],
  (urgencyLevels) => urgencyLevels.critical
);

/**
 * Select Urgent Urgency Count
 * 
 * Purpose: Get the count of urgent cases
 */
export const selectUrgentUrgencyCount = createSelector(
  [selectUrgencyLevels],
  (urgencyLevels) => urgencyLevels.urgent
);

/**
 * Select Routine Urgency Count
 * 
 * Purpose: Get the count of routine cases
 */
export const selectRoutineUrgencyCount = createSelector(
  [selectUrgencyLevels],
  (urgencyLevels) => urgencyLevels.routine
);

// ============================================================================
// FEEDBACK ANALYSIS SELECTORS
// ============================================================================

/**
 * Select Feedback Analysis Data
 * 
 * Purpose: Get the feedback analysis data
 */
export const selectFeedbackAnalysis = createSelector(
  [selectAIDashboardStats],
  (stats) => stats?.feedback_analysis || { positive: 0, negative: 0, total: 0 }
);

/**
 * Select Positive Feedback Count
 * 
 * Purpose: Get the count of positive feedbacks
 */
export const selectPositiveFeedbackCount = createSelector(
  [selectFeedbackAnalysis],
  (feedbackAnalysis) => feedbackAnalysis.positive
);

/**
 * Select Negative Feedback Count
 * 
 * Purpose: Get the count of negative feedbacks
 */
export const selectNegativeFeedbackCount = createSelector(
  [selectFeedbackAnalysis],
  (feedbackAnalysis) => feedbackAnalysis.negative
);

/**
 * Select Total Feedback Count
 * 
 * Purpose: Get the total count of feedbacks
 */
export const selectTotalFeedbackCount = createSelector(
  [selectFeedbackAnalysis],
  (feedbackAnalysis) => feedbackAnalysis.total
);

// ============================================================================
// TIME ANALYSIS SELECTORS
// ============================================================================

/**
 * Select Time Analysis Data
 * 
 * Purpose: Get the time-based analysis data
 */
export const selectTimeAnalysis = createSelector(
  [selectAIDashboardStats],
  (stats) => stats?.time_analysis || { today: 0, this_week: 0, this_month: 0, this_year: 0 }
);

/**
 * Select Today's Count
 * 
 * Purpose: Get the count for today
 */
export const selectTodayCount = createSelector(
  [selectTimeAnalysis],
  (timeAnalysis) => timeAnalysis.today
);

/**
 * Select This Week's Count
 * 
 * Purpose: Get the count for this week
 */
export const selectThisWeekCount = createSelector(
  [selectTimeAnalysis],
  (timeAnalysis) => timeAnalysis.this_week
);

/**
 * Select This Month's Count
 * 
 * Purpose: Get the count for this month
 */
export const selectThisMonthCount = createSelector(
  [selectTimeAnalysis],
  (timeAnalysis) => timeAnalysis.this_month
);

/**
 * Select This Year's Count
 * 
 * Purpose: Get the count for this year
 */
export const selectThisYearCount = createSelector(
  [selectTimeAnalysis],
  (timeAnalysis) => timeAnalysis.this_year
);

// ============================================================================
// LAST UPDATED SELECTORS
// ============================================================================

/**
 * Select Last Updated Timestamp
 * 
 * Purpose: Get the last updated timestamp
 */
export const selectLastUpdated = createSelector(
  [selectAIDashboardState],
  (aiDashboard) => aiDashboard.lastUpdated
);

// ============================================================================
// COMPUTED SELECTORS
// ============================================================================

/**
 * Select Dashboard Message
 * 
 * Purpose: Get the dashboard message or default message
 */
export const selectDashboardMessage = createSelector(
  [selectAIDashboardData],
  (dashboardData) => dashboardData?.message || 'Loading statistics...'
);

/**
 * Select Is Dashboard Empty
 * 
 * Purpose: Check if dashboard has no data
 */
export const selectIsDashboardEmpty = createSelector(
  [selectAIDashboardData],
  (dashboardData) => !dashboardData || !dashboardData.data
);

/**
 * Select Has Dashboard Data
 * 
 * Purpose: Check if dashboard has data
 */
export const selectHasDashboardData = createSelector(
  [selectIsDashboardEmpty],
  (isEmpty) => !isEmpty
);

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