import { useEffect, useMemo, useCallback } from 'react';
import { useNavigate } from 'react-router-dom';
import { Plus } from 'lucide-react';
import { useFAB } from '@/contexts/FABContext';

/**
 * Hook to configure FAB for different pages
 * Usage: useFABConfig('orders') in Orders page component
 */
export function useFABConfig(page: 'orders' | 'products' | 'customers' | 'coupons' | 'dashboard' | 'none') {
  const { setFAB, clearFAB } = useFAB();
  const navigate = useNavigate();

  // Memoize the icon to prevent re-creating on every render
  const icon = useMemo(() => <Plus className="w-6 h-6" />, []);

  // Memoize callbacks to prevent re-creating on every render
  const handleOrdersClick = useCallback(() => navigate('/orders/new'), [navigate]);
  const handleProductsClick = useCallback(() => navigate('/products/new'), [navigate]);
  const handleCustomersClick = useCallback(() => navigate('/customers/new'), [navigate]);
  const handleCouponsClick = useCallback(() => navigate('/coupons/new'), [navigate]);
  const handleDashboardClick = useCallback(() => {
    // TODO: Implement speed dial menu
    console.log('Quick actions menu');
  }, []);

  useEffect(() => {
    switch (page) {
      case 'orders':
        setFAB({
          icon,
          label: 'Create Order',
          onClick: handleOrdersClick,
          visible: true
        });
        break;

      case 'products':
        setFAB({
          icon,
          label: 'Add Product',
          onClick: handleProductsClick,
          visible: true
        });
        break;

      case 'customers':
        setFAB({
          icon,
          label: 'Add Customer',
          onClick: handleCustomersClick,
          visible: true
        });
        break;

      case 'coupons':
        setFAB({
          icon,
          label: 'Create Coupon',
          onClick: handleCouponsClick,
          visible: true
        });
        break;

      case 'dashboard':
        setFAB({
          icon,
          label: 'Quick Actions',
          onClick: handleDashboardClick,
          visible: true
        });
        break;

      case 'none':
      default:
        clearFAB();
        break;
    }

    return () => clearFAB();
  }, [page, icon, handleOrdersClick, handleProductsClick, handleCustomersClick, handleCouponsClick, handleDashboardClick, setFAB, clearFAB]);
}
