// DragonX Wallet - HushChat in-memory message store (implementation).

#include "chat_store.h"

namespace dragonx::chat {

std::string ChatStore::dedupKey(const ChatMessage& message) {
    return message.txid + ":" + std::to_string(message.payload_position);
}

bool ChatStore::append(const ChatMessage& message) {
    if (!seen_.insert(dedupKey(message)).second) return false;
    messages_.push_back(message);
    return true;
}

std::vector<ChatMessage> ChatStore::conversation(const std::string& conversationId) const {
    std::vector<ChatMessage> out;
    for (const auto& message : messages_) {
        if (message.conversation_id == conversationId) out.push_back(message);
    }
    return out;
}

std::vector<std::string> ChatStore::conversationIds() const {
    std::vector<std::string> ids;
    std::unordered_set<std::string> seenIds;
    for (const auto& message : messages_) {
        if (seenIds.insert(message.conversation_id).second) ids.push_back(message.conversation_id);
    }
    return ids;
}

void ChatStore::clear() {
    messages_.clear();
    seen_.clear();
}

} // namespace dragonx::chat
