import 'package:equatable/equatable.dart';

class AkinatorMessage extends Equatable {
  final String id;
  final String text;
  final bool isUser;
  final bool isTyping;
  final int confidenceScore;
  final String? sentimentLabel;
  final int sentimentScore;
  final String? critiqueNote;
  final Map<String, String> jargonDefinitions;
  final String? memo;
  final String? whatifResult;
  final bool ragUsed;
  final DateTime timestamp;

  const AkinatorMessage({
    required this.id,
    required this.text,
    this.isUser = false,
    this.isTyping = false,
    this.confidenceScore = 0,
    this.sentimentLabel,
    this.sentimentScore = 50,
    this.critiqueNote,
    this.jargonDefinitions = const {},
    this.memo,
    this.whatifResult,
    this.ragUsed = false,
    required this.timestamp,
  });

  bool get hasMetadata =>
      !isUser &&
      !isTyping &&
      (confidenceScore > 0 ||
          sentimentLabel != null ||
          (critiqueNote != null && critiqueNote!.isNotEmpty) ||
          jargonDefinitions.isNotEmpty ||
          (memo != null && memo!.isNotEmpty));

  @override
  List<Object?> get props => [id];
}

class AkinatorState extends Equatable {
  final List<AkinatorMessage> messages;
  final bool isTyping;
  final bool panelMode;
  final int? rateLimitSeconds;
  final String? selectedKbId;
  final String sessionId;
  final List<String> knowledgeBases;

  const AkinatorState({
    this.messages = const [],
    this.isTyping = false,
    this.panelMode = false,
    this.rateLimitSeconds,
    this.selectedKbId,
    required this.sessionId,
    this.knowledgeBases = const [],
  });

  bool get isRateLimited => rateLimitSeconds != null && rateLimitSeconds! > 0;

  AkinatorState copyWith({
    List<AkinatorMessage>? messages,
    bool? isTyping,
    bool? panelMode,
    int? Function()? rateLimitSeconds,
    String? Function()? selectedKbId,
    List<String>? knowledgeBases,
  }) {
    return AkinatorState(
      messages: messages ?? this.messages,
      isTyping: isTyping ?? this.isTyping,
      panelMode: panelMode ?? this.panelMode,
      rateLimitSeconds:
          rateLimitSeconds != null ? rateLimitSeconds() : this.rateLimitSeconds,
      selectedKbId:
          selectedKbId != null ? selectedKbId() : this.selectedKbId,
      sessionId: sessionId,
      knowledgeBases: knowledgeBases ?? this.knowledgeBases,
    );
  }

  @override
  List<Object?> get props =>
      [messages, isTyping, panelMode, rateLimitSeconds, selectedKbId, knowledgeBases];
}