import { useEffect } from "react";
import { trpc } from "@/lib/trpc";

export function BrowserTitleUpdater() {
  const agents = trpc.listAgents.useQuery(undefined, {
    refetchInterval: 10000,
  });

  const runningCount = agents.data?.filter((a) => a.status === "running").length ?? 0;
  const questionsCount = agents.data?.filter((a) => a.status === "waiting_for_input").length ?? 0;

  useEffect(() => {
    const parts: string[] = [];
    if (questionsCount > 0) parts.push(`${questionsCount} question${questionsCount > 1 ? "s" : ""}`);
    if (runningCount > 0) parts.push(`${runningCount} running`);

    document.title = parts.length > 0
      ? `(${parts.join(", ")}) Codewalkers`
      : "Codewalkers";
  }, [runningCount, questionsCount]);

  return null;
}
