import 'dart:convert'; import 'package:flutter/material.dart'; import 'tool_definition.dart'; /// Modal Confirm gate for destructive tools (ADR-0005 §OQ-3). /// /// Shown by [ToolDispatcher] right before invoking a destructive handler. /// Returns `true` only if the user explicitly tapped the confirm action; /// outside-tap / back-press / unmounted-context all return `false`. class ConfirmGate { const ConfirmGate(); Future show( BuildContext context, ToolDefinition tool, Map args, ) async { if (!context.mounted) return false; final summary = tool.summarize?.call(args) ?? _fallbackSummary(args); final result = await showDialog( context: context, barrierDismissible: true, builder: (ctx) { final theme = Theme.of(ctx); return AlertDialog( title: const Text('이 작업을 수행할까요?'), content: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(tool.description, style: theme.textTheme.bodyMedium), const SizedBox(height: 12), Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: theme.colorScheme.surfaceContainerHighest, borderRadius: BorderRadius.circular(8), ), child: Text(summary), ), ], ), actions: [ TextButton( onPressed: () => Navigator.of(ctx).pop(false), child: const Text('취소'), ), FilledButton( autofocus: true, onPressed: () => Navigator.of(ctx).pop(true), child: const Text('수행'), ), ], ); }, ); return result ?? false; } String _fallbackSummary(Map args) { try { return const JsonEncoder.withIndent(' ').convert(args); } catch (_) { return args.toString(); } } }