/*
 * File: useAIDashboard.ts
 * Description: Custom hook for AI dashboard functionality
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */

import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { AppDispatch } from '../../../store';
import { selectAIDashboardData, selectAIDashboardError, selectAIDashboardLoading, selectAIDashboardRefreshing, selectDashboardMessage } from '../redux/aiDashboardSelectors';
import { fetchAIDashboardStatistics, refreshAIDashboardStatistics } from '../redux/aiDashboardSlice';
import { selectUser } from '../../Auth/redux';

// import { 
//   fetchAIDashboardStatistics, 
//   refreshAIDashboardStatistics,
//   selectAIDashboardData,
//   selectAIDashboardLoading,
//   selectAIDashboardRefreshing,
//   selectAIDashboardError,
//   selectDashboardMessage
// } from '../redux';

/**
 * useAIDashboard Custom Hook
 * 
 * Purpose: Custom hook for AI dashboard functionality
 * 
 * Features:
 * - Fetch dashboard statistics from API
 * - Refresh dashboard data
 * - Access dashboard state from Redux
 * - Handle authentication token
 * 
 * @returns Object containing dashboard state and actions
 */
export const useAIDashboard = () => {
  const dispatch = useDispatch<AppDispatch>();
  
  // Select dashboard data from Redux store
  const dashboardData = useSelector(selectAIDashboardData);
  const isLoading = useSelector(selectAIDashboardLoading);
  const isRefreshing = useSelector(selectAIDashboardRefreshing);
  const error = useSelector(selectAIDashboardError);
  const dashboardMessage = useSelector(selectDashboardMessage);
  
  // TODO: Get actual authentication token from auth store
  // For now, using a placeholder token
  const authToken = useSelector(selectUser)?.access_token;
  
  /**
   * Fetch Dashboard Statistics
   * 
   * Purpose: Fetch dashboard statistics from API
   */
  const fetchDashboardStatistics = () => {
    dispatch(fetchAIDashboardStatistics(authToken));
  };
  
  /**
   * Refresh Dashboard Statistics
   * 
   * Purpose: Refresh dashboard statistics from API
   */
  const refreshDashboardStatistics = () => {
    dispatch(refreshAIDashboardStatistics(authToken));
  };
  
  /**
   * useEffect for initial data loading
   * 
   * Purpose: Load initial dashboard data from API when hook is used
   */
  useEffect(() => {
    // Fetch dashboard statistics from API
    fetchDashboardStatistics();
  }, []);
  
  return {
    // State
    dashboardData,
    isLoading,
    isRefreshing,
    error,
    dashboardMessage,
    
    // Actions
    fetchDashboardStatistics,
    refreshDashboardStatistics,
    
    // Constants
    authToken
  };
};

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