import React, { useMemo } from 'react';
import { Clock, DatabaseZap, Zap } from 'lucide-react-native';

import { useConversationFeatureNavigation } from '../../featureNavigationContext';
import type { SuperagentToolRendererProps } from '../../types';
import { getAutomationRowInfo } from '../automationWidgetUtils';
import { ToolWidgetRow } from '../primitives/ToolWidgetRow';
import { getSettledWidgetStatus, parseToolArgs } from '../toolWidgetUtils';

/**
 * list_automations / manage_automation — native port of the web
 * AutomationToolUI slim row: "{Verb} automation(s)" with the automation name
 * as a chip and an icon matching the automation type. Settled status keeps
 * stale rows from spinning forever (same correction as the card widgets).
 */
export function AutomationActionWidget({ isLastAssistantMessage, toolCall }: SuperagentToolRendererProps) {
  const openFeature = useConversationFeatureNavigation();
  const args = useMemo(() => parseToolArgs(toolCall), [toolCall]);
  const status = getSettledWidgetStatus(toolCall, isLastAssistantMessage);
  const info = getAutomationRowInfo(toolCall.name, args, status);
  const icon = info.type === 'scheduled' ? Clock : info.type === 'entity' ? DatabaseZap : Zap;
  // Stopped renders as an error row, matching the create/workflow widgets.
  const rowStatus = status === 'stopped' ? 'error' : status;

  return (
    <ToolWidgetRow
      accessibilityLabel="Open Automations settings"
      chipText={info.chipText}
      icon={icon}
      onPress={openFeature ? () => openFeature('automations') : undefined}
      status={rowStatus}
      title={info.title}
    />
  );
}
