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

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

  useEffect(() => {
    const from = new Date(year, month, 1).toISOString();
    const to = new Date(year, month + 1, 0, 23, 59, 59).toISOString();

    supabase
      .from('orders')
      .select('id, name, phone, status, scheduled_at, film_thickness, area_sqm')
      .gte('scheduled_at', from)
      .lte('scheduled_at', to)
      .order('scheduled_at', { ascending: true })
      .then(({ data }) => {
        setOrders(data || []);
        setLoading(false);
      });
  }, [year, month]);

  return { orders, loading };
}
