import { useState, useEffect } from 'react'

const API_BASE = import.meta.env.VITE_API_URL || '/api'

// Fetches the OCR models available for selection. Returns { models, loading }.
// Each model: { id, label, capabilities: { grounding, advanced_settings }, default }
export function useModels() {
  const [models, setModels] = useState([])
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    let cancelled = false
    fetch(`${API_BASE}/models`)
      .then(r => (r.ok ? r.json() : null))
      .then(data => {
        if (!cancelled && data?.models) setModels(data.models)
      })
      .catch(() => {})
      .finally(() => { if (!cancelled) setLoading(false) })
    return () => { cancelled = true }
  }, [])

  return { models, loading }
}
