import { useState, useEffect } from 'react';
import { supabase } from '../../lib/supabase';

export function useAnalyticsPage() {
  const [orders, setOrders] = useState([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    let isMounted = true;
    supabase
      .from('orders')
      .select('id, status, final_cost, created_at, assigned_to, film_thickness')
      .then(({ data }) => {
        if (isMounted) {
          setOrders(data ?? []);
          setLoading(false);
        }
      });
    return () => {
      isMounted = false;
    };
  }, []);

  return { orders, loading };
}
