"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.conversationManager = exports.ConversationManager = void 0;
const uuid_1 = require("uuid");
const index_js_1 = require("../data-store/index.js");
const logger_js_1 = require("../../utils/logger.js");
const log = (0, logger_js_1.createModuleLogger)('conversation-history');
const DEFAULT_HOURS_BACK = 72;
function createEmptyHistory(characterId, userId) {
    return {
        characterId,
        telegramUserId: userId,
        userInfo: { interests: [], notes: [] },
        messages: [],
        lastActivity: new Date().toISOString(),
        totalMessages: 0,
        totalStarsSpent: 0,
    };
}
class ConversationManager {
    async addMessage(characterId, userId, role, content, metadata, photoInfo) {
        let history = await index_js_1.dataStore.getConversationHistory(characterId, userId);
        if (!history) {
            history = createEmptyHistory(characterId, userId);
        }
        const message = {
            id: (0, uuid_1.v4)(),
            role,
            content,
            timestamp: new Date().toISOString(),
            ...(metadata ? { metadata } : {}),
            ...(photoInfo?.messageType ? { messageType: photoInfo.messageType } : {}),
            ...(photoInfo?.imagePath ? { imagePath: photoInfo.imagePath } : {}),
            ...(photoInfo?.imageUrl ? { imageUrl: photoInfo.imageUrl } : {}),
        };
        history.messages.push(message);
        history.totalMessages = history.messages.length;
        history.lastActivity = message.timestamp;
        await index_js_1.dataStore.saveConversationHistory(history);
        log.info({ characterId, userId, role, messageId: message.id, messageType: photoInfo?.messageType }, 'Message added');
        return history;
    }
    async getHistory(characterId, userId) {
        return index_js_1.dataStore.getConversationHistory(characterId, userId);
    }
    async getRecentMessages(characterId, userId, hoursBack = DEFAULT_HOURS_BACK) {
        const history = await index_js_1.dataStore.getConversationHistory(characterId, userId);
        if (!history)
            return [];
        const cutoff = new Date(Date.now() - hoursBack * 60 * 60 * 1000).toISOString();
        return history.messages.filter((m) => m.timestamp >= cutoff);
    }
    async updateUserInfo(characterId, userId, info) {
        let history = await index_js_1.dataStore.getConversationHistory(characterId, userId);
        if (!history) {
            history = createEmptyHistory(characterId, userId);
        }
        if (info.name !== undefined)
            history.userInfo.name = info.name;
        if (info.interests) {
            const existing = new Set(history.userInfo.interests ?? []);
            for (const i of info.interests)
                existing.add(i);
            history.userInfo.interests = [...existing];
        }
        if (info.notes) {
            const existing = new Set(history.userInfo.notes ?? []);
            for (const n of info.notes)
                existing.add(n);
            history.userInfo.notes = [...existing];
        }
        await index_js_1.dataStore.saveConversationHistory(history);
        log.info({ characterId, userId }, 'User info updated');
    }
    async deleteHistory(characterId, userId) {
        const deleted = await index_js_1.dataStore.deleteConversation(characterId, userId);
        if (deleted) {
            log.info({ characterId, userId }, 'Conversation deleted');
        }
        return deleted;
    }
}
exports.ConversationManager = ConversationManager;
exports.conversationManager = new ConversationManager();
//# sourceMappingURL=history.js.map