import { useState, useEffect } from 'react';
import { statsAPI } from '../utils/api';

export interface Stats {
  // User stats
  totalOrders?: number;
  itemsReceived?: number;
  lastPickup?: string | null;
  pendingOrders?: number;
  expiringItems?: number;
  
  // Admin stats
  totalUsers?: number;
  totalItems?: number;
  lowStockItems?: number;
}

export const useStats = () => {
  const [stats, setStats] = useState<Stats>({});
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  const fetchStats = async () => {
    try {
      setLoading(true);
      setError(null);
      const data = await statsAPI.get();
      setStats(data);
    } catch (err: any) {
      setError(err.message || 'Failed to fetch statistics');
      setStats({});
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    fetchStats();
  }, []);

  return { stats, loading, error, refetch: fetchStats };
};

