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

export interface PantrySettings {
  id: string;
  pantryName: string;
  contactPhone: string | null;
  address: string | null;
  lowStockThreshold: number;
  expiryWarningDays: number;
  createdAt: string;
  updatedAt: string;
}

export const useSettings = () => {
  const [settings, setSettings] = useState<PantrySettings | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  const fetchSettings = async () => {
    try {
      setLoading(true);
      setError(null);
      const data = await settingsAPI.get();
      setSettings(data.settings);
    } catch (err: any) {
      setError(err.message || 'Failed to fetch settings');
      console.error('Settings fetch error:', err);
    } finally {
      setLoading(false);
    }
  };

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

  return { settings, loading, error, refetch: fetchSettings };
};

