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

export function usePricing() {
  const [prices, setPrices] = useState({ 200: 2000, 300: 3000 });
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    supabase
      .from('settings')
      .select('key, value')
      .in('key', ['price_200', 'price_300'])
      .then(({ data, error }) => {
        if (!error && data) {
          const map = {};
          data.forEach(({ key, value }) => {
            const thickness = parseInt(key.replace('price_', ''), 10);
            map[thickness] = Number(value);
          });
          setPrices((prev) => ({ ...prev, ...map }));
        }
        setLoading(false);
      })
      .catch(() => setLoading(false));
  }, []);

  return { prices, loading };
}
