/**
 * Minimal `{id, name}` agent roster for the web tickets surface (ticket #86).
 *
 * NOT a reuse of `@/features/tickets/useProjectAgents`: that hook calls
 * `AgentGateway.listAgents` (`list_agents`), which is not in the web-server's
 * `/api/invoke` allowlist (desktop/Tauri-only surface — confirmed absent from
 * `crates/web-server/src/lib.rs`'s command dispatch). Adding it there is a
 * backend change outside this frontend-only lot.
 *
 * Workaround: derive the roster from `get_project_work_state`, which IS
 * allowlisted for web (the Live tab already depends on it) and already carries
 * `agentId` + `name` for every manifest agent. This only surfaces agents that
 * have a work-state row; on this codebase that is every declared project
 * agent, matching what `listAgents` would return.
 */

import { useMemo } from "react";

import { useProjectWorkState } from "@/features/workstate/useProjectWorkState";

export interface WebProjectAgent {
  id: string;
  name: string;
}

export interface WebProjectAgents {
  agents: WebProjectAgent[];
  nameOf: (agentId: string) => string;
  loaded: boolean;
}

export function useWebProjectAgents(projectId: string): WebProjectAgents {
  const vm = useProjectWorkState(projectId);
  const agents = useMemo(
    () => (vm.state?.agents ?? []).map((a) => ({ id: a.agentId, name: a.name })),
    [vm.state],
  );
  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, loaded: vm.state !== null };
}
