import 'package:flutter/material.dart';
import '../../../../core/theme/app_colors.dart';
import '../../../../core/theme/app_text_styles.dart';
import '../../../../core/theme/app_radius.dart';
import '../../../../core/widgets/chat_bubble.dart';
import '../../../../core/widgets/confidence_meter.dart';
import '../../../../core/widgets/sentiment_badge.dart';
import '../../../../core/widgets/jargon_panel.dart';
import '../../../../core/widgets/memo_card.dart';
import '../bloc/akinator_state.dart';

class AkinatorMessageCard extends StatelessWidget {
  final AkinatorMessage message;

  const AkinatorMessageCard({super.key, required this.message});

  @override
  Widget build(BuildContext context) {
    if (message.isUser) {
      return ChatBubble(text: message.text, isUser: true);
    }

    if (message.isTyping) {
      return const ChatBubble(text: '', isTyping: true, isAkinator: true);
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // Main bubble
        ChatBubble(
          text: message.text,
          isAkinator: true,
          ragUsed: message.ragUsed,
        ),

        // Metadata
        if (message.hasMetadata) ...[
          const SizedBox(height: 6),
          Padding(
            padding: const EdgeInsets.only(left: 4, right: 60),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // Confidence + Sentiment row
                if (message.confidenceScore > 0 ||
                    message.sentimentLabel != null)
                  Wrap(
                    spacing: 8,
                    runSpacing: 6,
                    children: [
                      if (message.confidenceScore > 0)
                        ConfidenceMeter(
                          score: message.confidenceScore,
                          size: 48,
                          strokeWidth: 4,
                        ),
                      if (message.sentimentLabel != null)
                        SentimentBadge(
                          label: message.sentimentLabel!,
                          score: message.sentimentScore,
                        ),
                    ],
                  ),

                // Critique
                if (message.critiqueNote != null &&
                    message.critiqueNote!.isNotEmpty) ...[
                  const SizedBox(height: 8),
                  Container(
                    padding: const EdgeInsets.all(10),
                    decoration: BoxDecoration(
                      color: AppColors.warningBg,
                      border: Border.all(
                          color: AppColors.warning.withOpacity(0.2)),
                      borderRadius: AppRadius.smAll,
                    ),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text('⚠️', style: TextStyle(fontSize: 14)),
                        const SizedBox(width: 6),
                        Expanded(
                          child: Text(
                            message.critiqueNote!,
                            style: AppTextStyles.bodySmall.copyWith(
                              color: AppColors.warningLight,
                              height: 1.5,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],

                // Jargon
                if (message.jargonDefinitions.isNotEmpty) ...[
                  const SizedBox(height: 8),
                  JargonPanel(definitions: message.jargonDefinitions),
                ],

                // Memo
                if (message.memo != null && message.memo!.isNotEmpty) ...[
                  const SizedBox(height: 8),
                  MemoCard(memo: message.memo!),
                ],
              ],
            ),
          ),
        ],
      ],
    );
  }
}