import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../domain/chat_shells.dart'; import '../domain/chat_task_metadata.dart'; import 'chat_task_metadata_view.dart'; import 'chat_task_presentation.dart'; /// One scan-friendly task card; only the explicit Details control loads output. class ChatBackgroundTaskCard extends StatelessWidget { const ChatBackgroundTaskCard({ super.key, required this.shell, required this.now, required this.selected, required this.onDetails, this.detail, }); final JsonMap shell; final DateTime now; final bool selected; final VoidCallback onDetails; final Widget? detail; @override Widget build(BuildContext context) { final cupertino = taskCupertino(context); final kind = taskKindLabel(shell); final agent = '${shell['task_type'] ?? ''}'.contains('agent'); final progress = distinctTaskProgress(shell); final elapsed = taskElapsed(shell, now); final accent = cupertino ? CupertinoTheme.of(context).primaryColor : Theme.of(context).colorScheme.primary; final status = DecoratedBox( decoration: BoxDecoration( color: cupertino ? CupertinoColors.tertiarySystemFill.resolveFrom(context) : Theme.of(context).colorScheme.secondaryContainer, borderRadius: BorderRadius.circular(8), ), child: Padding( padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5), child: Row( mainAxisSize: MainAxisSize.min, children: [ Icon( cupertino ? CupertinoIcons.play_fill : Icons.play_arrow_rounded, size: 14, color: accent, ), const SizedBox(width: 4), Flexible( child: Text( 'Running', style: cupertino ? CupertinoTheme.of(context).textTheme.textStyle.copyWith( fontSize: 13, fontWeight: FontWeight.w500, color: CupertinoColors.label.resolveFrom(context), ) : Theme.of(context).textTheme.labelMedium?.copyWith( color: Theme.of( context, ).colorScheme.onSecondaryContainer, ), ), ), ], ), ), ); final details = Row( mainAxisSize: MainAxisSize.min, children: [ Flexible(child: Text(selected ? 'Hide details' : 'Details')), const SizedBox(width: 4), Icon( cupertino ? (selected ? CupertinoIcons.chevron_up : CupertinoIcons.chevron_down) : (selected ? Icons.expand_less : Icons.expand_more), size: 18, ), ], ); return ChatTaskSurface( key: ValueKey('chat-bg-task-card-${shellIdOf(shell)}'), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ CircleAvatar( backgroundColor: cupertino ? CupertinoColors.tertiarySystemFill.resolveFrom(context) : Theme.of(context).colorScheme.primaryContainer, child: Icon( agent ? (cupertino ? CupertinoIcons.person_2 : Icons.smart_toy_outlined) : (cupertino ? CupertinoIcons.chevron_left_slash_chevron_right : Icons.terminal), color: accent, ), ), const SizedBox(width: 12), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ if (kind != null) Text( kind, style: TextStyle( color: taskSecondary(context), fontSize: 12, ), ), Text( taskDisplayTitle(shell), style: taskHeading(context), maxLines: selected ? null : 2, overflow: selected ? TextOverflow.clip : TextOverflow.ellipsis, ), ], ), ), ], ), const SizedBox(height: 16), Wrap( alignment: WrapAlignment.spaceBetween, crossAxisAlignment: WrapCrossAlignment.center, spacing: 16, runSpacing: 8, children: [ status, if (elapsed != null) Semantics( label: 'Elapsed ${taskDuration(elapsed)}', child: Text( taskDuration(elapsed), style: (cupertino ? CupertinoTheme.of(context) .textTheme .navLargeTitleTextStyle .copyWith(fontSize: 24) : Theme.of(context).textTheme.headlineSmall), ), ), ], ), const SizedBox(height: 20), ChatTaskMetadataView(shell: shell, now: now), if (progress != null) ...[ const SizedBox(height: 16), DecoratedBox( decoration: BoxDecoration( color: cupertino ? CupertinoColors.tertiarySystemGroupedBackground .resolveFrom(context) : Theme.of(context).colorScheme.surfaceContainerLowest, borderRadius: BorderRadius.circular(8), ), child: Padding( padding: const EdgeInsets.all(12), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Icon( cupertino ? CupertinoIcons.text_bubble : Icons.notes_rounded, size: 18, color: taskSecondary(context), ), const SizedBox(width: 8), Expanded( child: Text( progress, maxLines: selected ? null : 2, overflow: selected ? TextOverflow.clip : TextOverflow.ellipsis, ), ), ], ), ), ), ], Align( alignment: AlignmentDirectional.centerEnd, child: Semantics( expanded: selected, child: cupertino ? CupertinoButton( key: ValueKey('chat-bg-task-${shellIdOf(shell)}'), sizeStyle: CupertinoButtonSize.small, onPressed: onDetails, child: details, ) : TextButton( key: ValueKey('chat-bg-task-${shellIdOf(shell)}'), onPressed: onDetails, child: details, ), ), ), if (detail != null) ...[ if (cupertino) Container( height: 0.5, color: CupertinoColors.separator.resolveFrom(context), ) else const Divider(), const SizedBox(height: 12), detail!, ], ], ), ); } }