import { ListTodoIcon } from "lucide-react";
import { useTranslation } from "react-i18next";
import { TABS_TASK_NAMESPACE } from "@/i18n/resources";
import type { ExecutionControlUpdateTodos } from "@/api/generated/schemas";
import { getToolMessageMetadata } from "@/types/message";
import { UpdateTodosSchema } from "@/api/tool-schema";
import { TodoList } from "@/features/Tabs/TaskPanel/components/TodoList";
import type { ToolMessageProps } from ".";
import {
  BuiltInToolContainer,
  BuiltInToolContent,
  BuiltInToolHeader,
} from "./components/BuiltInTool";
import { ToolConfirmation } from "./components/ToolConfirmation";
import { useAgentTaskAction } from "../../../hooks/use-agent-task";
import { useToolArgument } from "../../../hooks/use-tool-argument";
import { useToolActionable } from "../../../hooks/use-tool-actionable";

export function UpdateTodos({ message }: ToolMessageProps) {
  const { t } = useTranslation(TABS_TASK_NAMESPACE);
  const { reviewTool } = useAgentTaskAction();
  const toolArguments = useToolArgument<ExecutionControlUpdateTodos>(
    message,
    UpdateTodosSchema,
  );
  const { disabled, markAsSubmitted } = useToolActionable(message);
  const todos = toolArguments?.todos ?? [];
  const { userApproval, risk } = getToolMessageMetadata(message);

  const content = (() => {
    if (message.isStreaming) {
      return (
        <p className="px-4 pb-4 text-muted-foreground text-sm">
          {t("tool.update_todos.generating")}
        </p>
      );
    }
    if (toolArguments === null) {
      return (
        <p className="px-4 pb-4 text-muted-foreground text-sm">
          {t("tool.update_todos.parse_error")}
        </p>
      );
    }
    if (todos.length === 0) {
      return (
        <p className="px-4 pb-4 text-muted-foreground text-sm">
          {t("tool.update_todos.empty")}
        </p>
      );
    }
    return (
      <div className="px-2 pb-4">
        <TodoList todos={todos} />
      </div>
    );
  })();

  return (
    <BuiltInToolContainer id={message.call_id} defaultOpen>
      <BuiltInToolHeader
        title={t("tool.update_todos.title")}
        icon={ListTodoIcon}
        risk={risk}
      />
      <BuiltInToolContent>{content}</BuiltInToolContent>
      {userApproval && (
        <ToolConfirmation
          state={userApproval}
          disabled={disabled}
          riskLevel={risk.level}
          onSubmit={markAsSubmitted}
          onAccept={() => reviewTool(message.call_id, "approved")}
          onReject={() => reviewTool(message.call_id, "denied")}
        />
      )}
    </BuiltInToolContainer>
  );
}
