/**
 * Minimal read-only list of a project's agents, used by the ticket surfaces to
 * (a) resolve agent ids to names and (b) offer only *known* agents when
 * assigning (F5). Deliberately lighter than the full `useAgents` view-model —
 * tickets never launch or mutate agents.
 */

import { useEffect, useMemo, useState } from "react";

import type { Agent } from "@/domain";
import { useGateways } from "@/app/di";

export interface ProjectAgents {
  agents: Agent[];
  /** Maps an agent id to a display name, falling back to a short id. */
  nameOf: (agentId: string) => string;
}

export function useProjectAgents(projectId: string): ProjectAgents {
  const { agent } = useGateways();
  const [agents, setAgents] = useState<Agent[]>([]);

  useEffect(() => {
    let cancelled = false;
    void agent
      .listAgents(projectId)
      .then((list) => {
        if (!cancelled) setAgents(list);
      })
      .catch(() => {
        if (!cancelled) setAgents([]);
      });
    return () => {
      cancelled = true;
    };
  }, [projectId, agent]);

  const byId = useMemo(
    () => new Map(agents.map((a) => [a.id, a.name])),
    [agents],
  );

  const nameOf = (agentId: string): string =>
    byId.get(agentId) ?? `${agentId.slice(0, 8)}…`;

  return { agents, nameOf };
}
