#pragma once

// DragonX Wallet - HushChat in-memory message store: the fast read model / dedup view. Durable
// persistence lives in ChatDatabase; ChatService rehydrates this store from it on unlock.

#include "chat_message.h"

#include <string>
#include <unordered_set>
#include <vector>

namespace dragonx::chat {

// Threads messages by conversation_id (cid) and deduplicates by (txid, payload_position) so
// re-scanning the chain never double-inserts. Not thread-safe — drive from the main thread.
class ChatStore {
public:
    // Returns true if newly inserted, false if a duplicate was ignored.
    bool append(const ChatMessage& message);

    // Messages in a conversation, in insertion order.
    std::vector<ChatMessage> conversation(const std::string& conversationId) const;

    // Distinct conversation ids, in first-seen order.
    std::vector<std::string> conversationIds() const;

    std::size_t size() const { return messages_.size(); }
    bool empty() const { return messages_.empty(); }
    void clear();

private:
    static std::string dedupKey(const ChatMessage& message);

    std::vector<ChatMessage> messages_;
    std::unordered_set<std::string> seen_;
};

} // namespace dragonx::chat
