/*
 * File: hooks.ts
 * Description: Custom Redux hooks for easy store access
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */

import { useDispatch, useSelector, TypedUseSelectorHook } from 'react-redux';
import { RootState, AppDispatch } from './index';

// ============================================================================
// TYPED HOOKS
// ============================================================================

/**
 * Typed Use Dispatch Hook
 * 
 * Purpose: Provide typed dispatch function
 * 
 * Features:
 * - Type-safe dispatch function
 * - Proper TypeScript support
 * - Consistent with Redux Toolkit patterns
 */
export const useAppDispatch = () => useDispatch<AppDispatch>();

/**
 * Typed Use Selector Hook
 * 
 * Purpose: Provide typed selector function
 * 
 * Features:
 * - Type-safe selector function
 * - Proper TypeScript support
 * - Consistent with Redux Toolkit patterns
 */
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

// ============================================================================
// AUTH HOOKS
// ============================================================================

/**
 * Use Auth Hook
 * 
 * Purpose: Get authentication state and actions
 * 
 * @returns Authentication state and actions
 */
export const useAuth = () => {
  const auth = useAppSelector((state) => state.auth);
  const dispatch = useAppDispatch();
  
  return {
    // State
    user: auth.user,
    isAuthenticated: auth.isAuthenticated,
    isLoading: auth.isLoading,
    isLoggingIn: auth.isLoggingIn,
    isLoggingOut: auth.isLoggingOut,
    error: auth.error,
    loginError: auth.loginError,
    
    // Actions
    dispatch,
  };
};

/**
 * Use User Hook
 * 
 * Purpose: Get current user information
 * 
 * @returns Current user data
 */
export const useUser = () => {
  const user = useAppSelector((state) => state.auth.user);
  
  return {
    user,
    isLoggedIn: !!user,
    hasPermission: (permission: string) => user?.permissions?.includes(permission) || false,
  };
};

// ============================================================================
// DASHBOARD HOOKS
// ============================================================================

/**
 * Use Dashboard Hook
 * 
 * Purpose: Get dashboard state and actions
 * 
 * @returns Dashboard state and actions
 */
export const useDashboard = () => {
  const dashboard = useAppSelector((state) => state.dashboard);
  const dispatch = useAppDispatch();
  
  return {
    // State
    dashboard: dashboard.dashboard,
    isLoading: dashboard.isLoading,
    isRefreshing: dashboard.isRefreshing,
    error: dashboard.error,
    lastUpdated: dashboard.lastUpdated,
    isConnected: dashboard.isConnected,
    selectedFilter: dashboard.selectedFilter,
    sortBy: dashboard.sortBy,
    sortOrder: dashboard.sortOrder,
    
    // Actions
    dispatch,
  };
};

// ============================================================================
// ALERTS HOOKS
// ============================================================================

/**
 * Use Alerts Hook
 * 
 * Purpose: Get alerts state and actions
 * 
 * @returns Alerts state and actions
 */
export const useAlerts = () => {
  const alerts = useAppSelector((state) => state.alerts);
  const dispatch = useAppDispatch();
  
  return {
    // State
    alerts: alerts.alerts,
    isLoading: alerts.isLoading,
    isRefreshing: alerts.isRefreshing,
    error: alerts.error,
    lastUpdated: alerts.lastUpdated,
    unreadCount: alerts.unreadCount,
    criticalCount: alerts.criticalCount,
    selectedFilter: alerts.selectedFilter,
    sortBy: alerts.sortBy,
    sortOrder: alerts.sortOrder,
    
    // Computed values
    criticalAlerts: alerts.alerts.filter(alert => alert.priority === 'CRITICAL'),
    unreadAlerts: alerts.alerts.filter(alert => !alert.isRead),
    acknowledgedAlerts: alerts.alerts.filter(alert => alert.isAcknowledged),
    
    // Actions
    dispatch,
  };
};

// ============================================================================
// PATIENT CARE HOOKS
// ============================================================================

/**
 * Use Patient Care Hook
 * 
 * Purpose: Get patient care state and actions
 * 
 * @returns Patient care state and actions
 */
export const usePatientCare = () => {
  const patientCare = useAppSelector((state) => state.patientCare);
  const dispatch = useAppDispatch();
  
  return {
    // State
    patients: patientCare.patients,
    currentPatient: patientCare.currentPatient,
    isLoading: patientCare.isLoading,
    isRefreshing: patientCare.isRefreshing,
    isLoadingPatientDetails: patientCare.isLoadingPatientDetails,
    error: patientCare.error,
    searchQuery: patientCare.searchQuery,
    selectedFilter: patientCare.selectedFilter,
    sortBy: patientCare.sortBy,
    sortOrder: patientCare.sortOrder,
    currentPage: patientCare.currentPage,
    itemsPerPage: patientCare.itemsPerPage,
    totalItems: patientCare.totalItems,
    lastUpdated: patientCare.lastUpdated,
    
    // Computed values
    activePatients: patientCare.patients.filter(patient => patient.status === 'ACTIVE'),
    criticalPatients: patientCare.patients.filter(patient => patient.priority === 'CRITICAL'),
    dischargedPatients: patientCare.patients.filter(patient => patient.status === 'DISCHARGED'),
    
    // Actions
    dispatch,
  };
};

// ============================================================================
// SETTINGS HOOKS
// ============================================================================

/**
 * Use Settings Hook
 * 
 * Purpose: Get settings state and actions
 * 
 * @returns Settings state and actions
 */
export const useSettings = () => {
  const settings = useAppSelector((state) => state.settings);
  const dispatch = useAppDispatch();
  
  return {
    // State
    userProfile: settings.userProfile,
    userPreferences: settings.userPreferences,
    isLoading: settings.isLoading,
    isUpdatingProfile: settings.isUpdatingProfile,
    isUpdatingPreferences: settings.isUpdatingPreferences,
    error: settings.error,
    isProfileValid: settings.isProfileValid,
    isPreferencesValid: settings.isPreferencesValid,
    profileLastUpdated: settings.profileLastUpdated,
    preferencesLastUpdated: settings.preferencesLastUpdated,
    
    // Actions
    dispatch,
  };
};

// ============================================================================
// UI HOOKS
// ============================================================================

/**
 * Use UI Hook
 * 
 * Purpose: Get UI state and actions
 * 
 * @returns UI state and actions
 */
export const useUI = () => {
  const ui = useAppSelector((state) => state.ui);
  const dispatch = useAppDispatch();
  
  return {
    // State
    isLoading: ui.isLoading,
    loadingMessage: ui.loadingMessage,
    isModalOpen: ui.isModalOpen,
    modalType: ui.modalType,
    modalData: ui.modalData,
    isOverlayVisible: ui.isOverlayVisible,
    overlayType: ui.overlayType,
    currentScreen: ui.currentScreen,
    navigationStack: ui.navigationStack,
    isDarkMode: ui.isDarkMode,
    fontSize: ui.fontSize,
    highContrast: ui.highContrast,
    isRefreshing: ui.isRefreshing,
    isScrolling: ui.isScrolling,
    lastInteraction: ui.lastInteraction,
    hasError: ui.hasError,
    errorMessage: ui.errorMessage,
    errorType: ui.errorType,
    
    // Actions
    dispatch,
  };
};

// ============================================================================
// UTILITY HOOKS
// ============================================================================

/**
 * Use Store Hook
 * 
 * Purpose: Get access to the entire store state
 * 
 * @returns Entire store state
 */
export const useStore = () => {
  return useAppSelector((state) => state);
};

/**
 * Use Persistence Status Hook
 * 
 * Purpose: Get Redux Persist status
 * 
 * @returns Persistence status
 */
export const usePersistenceStatus = () => {
  // This would need to be implemented with a custom selector
  // that accesses the persistor state
  return {
    isRehydrated: true, // Placeholder
    isPersisting: false, // Placeholder
  };
};

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