import 'package:flutter/material.dart';
import 'package:gpt_markdown/gpt_markdown.dart';
import '../../../../core/theme/app_colors.dart';

/// Widget that renders streaming text as real-time Markdown.
///
/// During streaming: renders Markdown with a solid block cursor at the end.
/// When streaming completes: renders Markdown with text selection enabled.
class StreamTextWidget extends StatelessWidget {
  final String text;
  final bool isStreaming;
  final TextStyle? style;

  const StreamTextWidget({
    super.key,
    required this.text,
    this.isStreaming = true,
    this.style,
  });

  @override
  Widget build(BuildContext context) {
    final effectiveStyle = style ??
        const TextStyle(
          color: AppColors.textPrimary,
          fontSize: 15,
        );

    // Streaming: append solid block cursor; completed: plain markdown
    final displayText = isStreaming ? '$text\u2588' : text;

    return RepaintBoundary(
      child: isStreaming
          ? GptMarkdown(displayText, style: effectiveStyle)
          : SelectionArea(
              child: GptMarkdown(displayText, style: effectiveStyle),
            ),
    );
  }
}
