import 'package:flutter/material.dart';
import '../../../../core/theme/app_colors.dart';
import '../../../../core/widgets/risk_level_chip.dart';
import '../../domain/entities/chat_message.dart';

/// Card widget that displays tool execution details including
/// tool name, input, output, and risk level.
class ToolExecutionCard extends StatelessWidget {
  final ToolExecution toolExecution;

  const ToolExecutionCard({super.key, required this.toolExecution});

  Color get _statusColor {
    switch (toolExecution.status) {
      case ToolStatus.executing:
        return AppColors.info;
      case ToolStatus.completed:
        return AppColors.success;
      case ToolStatus.error:
        return AppColors.error;
      case ToolStatus.blocked:
        return AppColors.warning;
      case ToolStatus.awaitingApproval:
        return AppColors.warning;
    }
  }

  IconData get _statusIcon {
    switch (toolExecution.status) {
      case ToolStatus.executing:
        return Icons.play_circle_outline;
      case ToolStatus.completed:
        return Icons.check_circle_outline;
      case ToolStatus.error:
        return Icons.error_outline;
      case ToolStatus.blocked:
        return Icons.block;
      case ToolStatus.awaitingApproval:
        return Icons.hourglass_top;
    }
  }

  String get _statusLabel {
    switch (toolExecution.status) {
      case ToolStatus.executing:
        return '执行中';
      case ToolStatus.completed:
        return '已完成';
      case ToolStatus.error:
        return '错误';
      case ToolStatus.blocked:
        return '已阻止';
      case ToolStatus.awaitingApproval:
        return '等待审批';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(vertical: 6),
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: AppColors.surfaceLight.withOpacity(0.5),
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: _statusColor.withOpacity(0.3)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Header row: tool name + status + risk level
          Row(
            children: [
              Icon(Icons.code, size: 16, color: _statusColor),
              const SizedBox(width: 6),
              Expanded(
                child: Text(
                  toolExecution.toolName,
                  style: TextStyle(
                    color: _statusColor,
                    fontSize: 13,
                    fontWeight: FontWeight.w600,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              const SizedBox(width: 8),
              Icon(_statusIcon, size: 14, color: _statusColor),
              const SizedBox(width: 4),
              Text(
                _statusLabel,
                style: TextStyle(color: _statusColor, fontSize: 11),
              ),
              if (toolExecution.riskLevel > 0) ...[
                const SizedBox(width: 8),
                RiskLevelChip(level: toolExecution.riskLevel),
              ],
            ],
          ),

          const SizedBox(height: 8),

          // Input
          if (toolExecution.input.isNotEmpty) ...[
            const Text(
              '输入',
              style: TextStyle(
                color: AppColors.textMuted,
                fontSize: 11,
                fontWeight: FontWeight.w500,
              ),
            ),
            const SizedBox(height: 4),
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(10),
              decoration: BoxDecoration(
                color: AppColors.background,
                borderRadius: BorderRadius.circular(8),
              ),
              child: SelectableText(
                toolExecution.input,
                style: const TextStyle(
                  fontFamily: 'monospace',
                  fontSize: 12,
                  color: AppColors.textSecondary,
                ),
              ),
            ),
          ],

          // Output
          if (toolExecution.output != null && toolExecution.output!.isNotEmpty) ...[
            const SizedBox(height: 8),
            const Text(
              '输出',
              style: TextStyle(
                color: AppColors.textMuted,
                fontSize: 11,
                fontWeight: FontWeight.w500,
              ),
            ),
            const SizedBox(height: 4),
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(10),
              constraints: const BoxConstraints(maxHeight: 200),
              decoration: BoxDecoration(
                color: AppColors.background,
                borderRadius: BorderRadius.circular(8),
              ),
              child: SingleChildScrollView(
                child: SelectableText(
                  toolExecution.output!,
                  style: TextStyle(
                    fontFamily: 'monospace',
                    fontSize: 12,
                    color: toolExecution.status == ToolStatus.error
                        ? AppColors.error
                        : AppColors.textSecondary,
                  ),
                ),
              ),
            ),
          ],

          // Loading indicator for executing status
          if (toolExecution.status == ToolStatus.executing) ...[
            const SizedBox(height: 8),
            const LinearProgressIndicator(minHeight: 2),
          ],
        ],
      ),
    );
  }
}
