"use client";

import { useState, useEffect } from "react";
import type { AgentConfig } from "@/lib/types";

interface UseAgentsReturn {
  agents: AgentConfig[];
  loading: boolean;
}

export function useAgents(): UseAgentsReturn {
  const [agents, setAgents] = useState<AgentConfig[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetch("/api/agents")
      .then((r) => r.json())
      .then((data: { agents: AgentConfig[] }) => {
        setAgents(data.agents);
      })
      .catch(() => {
        setAgents([]);
      })
      .finally(() => setLoading(false));
  }, []);

  return { agents, loading };
}
