import { useState, useEffect, useCallback } from 'react'
import { integrationsApi } from '../lib/integrationsApi'

export function useFinanceSummary() {
  const [summary, setSummary] = useState(null)
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState(null)

  const load = useCallback(async () => {
    setLoading(true)
    setError(null)
    try {
      setSummary(await integrationsApi.invoicesSummary())
    } catch (err) {
      setError(err.message)
    } finally {
      setLoading(false)
    }
  }, [])

  useEffect(() => { load() }, [load])

  return { summary, loading, error, refresh: load }
}
