enum ChatRole { user, assistant, system }

extension ChatRoleExt on ChatRole {
  String enumToStr() => toString().split(".").last;
  bool equalsTo(ChatRole role) => toString() == role.toString();
  bool equalsStringTo(String role) => enumToStr() == role;
}

class OpenAIChatMessage {
  late ChatRole role; // JSON of ChatMessage
  late String content;

  OpenAIChatMessage(this.role, this.content, {String? name});

  OpenAIChatMessage.fromJson(Map json) {
    role = toChatRole(json['role']);
    content = json['content'];
  }

  ChatRole toChatRole(String strRole) {
    ChatRole role = ChatRole.user;
    for (ChatRole chatRole in ChatRole.values) {
      if (chatRole.equalsStringTo(strRole)) {
        role = chatRole;
        break;
      }
    }
    return role;
  }

  Map<String, dynamic> toJson() => {
        "role": role.enumToStr(),
        "content": content,
      };

  @override
  String toString() => toJson().toString();
}