import 'tool_result.dart';

enum ChatItemType { message, toolCall, toolResult }

enum MessageSender { user, ai }

enum ToolCallStatus { pending, executing, completed, error }

abstract class ChatListItem {
  String get id;
  DateTime get timestamp;
  ChatItemType get type;
  MessageSender get sender;
}

class TextMessageItem extends ChatListItem {
  @override
  final String id;
  final String content;
  @override
  final DateTime timestamp;
  @override
  final MessageSender sender;
  final bool isStreaming;

  TextMessageItem({
    required this.id,
    required this.content,
    required this.timestamp,
    required this.sender,
    this.isStreaming = false,
  });

  @override
  ChatItemType get type => ChatItemType.message;

  TextMessageItem copyWith({
    String? id,
    String? content,
    DateTime? timestamp,
    MessageSender? sender,
    bool? isStreaming,
  }) => TextMessageItem(
    id: id ?? this.id,
    content: content ?? this.content,
    timestamp: timestamp ?? this.timestamp,
    sender: sender ?? this.sender,
    isStreaming: isStreaming ?? this.isStreaming,
  );
}

class ToolCallItem extends ChatListItem {
  @override
  final String id;
  final String callId;
  final String toolName;
  final Map<String, dynamic> args;
  final ToolCallStatus status;
  final String? errorMessage;
  @override
  final DateTime timestamp;
  @override
  final MessageSender sender;

  ToolCallItem({
    required this.id,
    required this.callId,
    required this.toolName,
    required this.args,
    required this.status,
    this.errorMessage,
    required this.timestamp,
    required this.sender,
  });

  @override
  ChatItemType get type => ChatItemType.toolCall;

  ToolCallItem copyWith({
    String? id,
    String? callId,
    String? toolName,
    Map<String, dynamic>? args,
    ToolCallStatus? status,
    String? errorMessage,
    DateTime? timestamp,
    MessageSender? sender,
  }) => ToolCallItem(
    id: id ?? this.id,
    callId: callId ?? this.callId,
    toolName: toolName ?? this.toolName,
    args: args ?? this.args,
    status: status ?? this.status,
    errorMessage: errorMessage ?? this.errorMessage,
    timestamp: timestamp ?? this.timestamp,
    sender: sender ?? this.sender,
  );
}

class ToolResultItem extends ChatListItem {
  @override
  final String id;
  final String callId;
  final UiCard uiCard;
  @override
  final DateTime timestamp;
  @override
  final MessageSender sender;

  ToolResultItem({
    required this.id,
    required this.callId,
    required this.uiCard,
    required this.timestamp,
    required this.sender,
  });

  @override
  ChatItemType get type => ChatItemType.toolResult;
}
