import React, { useMemo } from 'react';
import { Text, View } from 'react-native';
import { Clock, Database, Webhook, Zap, type LucideIcon } from 'lucide-react-native';

import { useConversationFeatureNavigation } from '../../featureNavigationContext';
import { themedColor } from '../../theme';
import type { SuperagentToolRendererProps } from '../../types';
import { describeTrigger, getAutomationType } from '../automationWidgetUtils';
import { ToolWidgetRow } from '../primitives/ToolWidgetRow';
import { WidgetCard } from '../primitives/WidgetCard';
import { toolWidgetStyles } from '../primitives/toolWidgetStyles';
import { getSettledWidgetStatus, getStringArg, parseToolArgs } from '../toolWidgetUtils';
import { ActiveBadge, AutomationCardFooter } from './automationCardParts';

const TYPE_ICONS: Record<string, LucideIcon> = {
  scheduled: Clock,
  entity: Database,
  connector: Webhook,
};

/**
 * create_automation — native port of the web CreateAutomationWidget task card
 * (the agent-builder variant): name + Active/Paused badge, trigger summary,
 * description, and the "runs when triggered" / credits footer. The web card
 * navigates to the Tasks tab on tap.
 */
export function CreateAutomationWidget({ agent, isLastAssistantMessage, toolCall }: SuperagentToolRendererProps) {
  const openFeature = useConversationFeatureNavigation();
  const args = useMemo(() => parseToolArgs(toolCall), [toolCall]);
  const status = getSettledWidgetStatus(toolCall, isLastAssistantMessage);
  const name = getStringArg(args, ['name']);

  if (status === 'running') {
    return <ToolWidgetRow chipText={name || null} icon={Zap} status="running" title="Creating automation..." />;
  }
  if (status === 'error' || status === 'stopped') {
    return <ToolWidgetRow icon={Zap} status="error" title="Failed to create automation" />;
  }

  const trigger = describeTrigger(args);
  const description = getStringArg(args, ['description']);
  const TypeIcon = TYPE_ICONS[getAutomationType(args) ?? ''] ?? Zap;
  const isActive = args?.is_active !== false;

  return (
    <WidgetCard
      accessibilityLabel="Open Automations settings"
      onPress={openFeature ? () => openFeature('automations') : undefined}
    >
      <View style={toolWidgetStyles.cardBody}>
        <View style={toolWidgetStyles.cardHeader}>
          <View style={toolWidgetStyles.cardIconBox}>
            <TypeIcon color={themedColor('#A1A1AA')} size={15} strokeWidth={2.3} />
          </View>
          <View style={toolWidgetStyles.cardHeaderMeta}>
            <View style={toolWidgetStyles.cardTitleRow}>
              <Text numberOfLines={1} style={toolWidgetStyles.cardTitle}>{name || 'Automation'}</Text>
              <ActiveBadge isActive={isActive} />
            </View>
            {trigger ? <Text style={toolWidgetStyles.cardSubtitle}>{trigger}</Text> : null}
          </View>
        </View>

        {description ? (
          <Text numberOfLines={3} style={toolWidgetStyles.cardDescription}>{description}</Text>
        ) : null}

        <AutomationCardFooter agentName={agent.name} />
      </View>
    </WidgetCard>
  );
}
