import { v4 as uuid } from 'uuid';
import { join } from 'node:path';
import { readJsonFile, writeJsonFile, getDataDir } from '../storage/json-store.js';
function messageFilePath(workspaceId, channelId) {
    return join(getDataDir(), 'workspaces', workspaceId, 'channels', channelId, 'messages.json');
}
export function listMessages(workspaceId, channelId, opts = {}) {
    const all = readJsonFile(messageFilePath(workspaceId, channelId)) || [];
    const limit = opts.limit || 50;
    if (opts.before) {
        const idx = all.findIndex((m) => m.id === opts.before);
        if (idx <= 0)
            return [];
        return all.slice(Math.max(0, idx - limit), idx);
    }
    return all.slice(-limit);
}
export function createMessage(workspaceId, channelId, data) {
    const path = messageFilePath(workspaceId, channelId);
    const messages = readJsonFile(path) || [];
    const message = {
        id: uuid(),
        channelId,
        senderId: data.senderId,
        senderRole: data.senderRole,
        content: data.content,
        type: data.type || 'text',
        status: data.status,
        attachments: data.attachments,
        parts: data.parts,
        metadata: data.metadata,
        createdAt: new Date().toISOString(),
    };
    messages.push(message);
    writeJsonFile(path, messages);
    return message;
}
export function updateMessage(workspaceId, channelId, messageId, data) {
    const path = messageFilePath(workspaceId, channelId);
    const messages = readJsonFile(path) || [];
    const index = messages.findIndex((message) => message.id === messageId);
    if (index === -1)
        return null;
    const updated = {
        ...messages[index],
        ...data,
    };
    messages[index] = updated;
    writeJsonFile(path, messages);
    return updated;
}
export function appendMessageReply(workspaceId, channelId, messageId, data) {
    const path = messageFilePath(workspaceId, channelId);
    const messages = readJsonFile(path) || [];
    const index = messages.findIndex((message) => message.id === messageId);
    if (index === -1)
        return null;
    const reply = {
        id: uuid(),
        senderId: data.senderId,
        senderRole: data.senderRole,
        content: data.content,
        status: data.status,
        attachments: data.attachments,
        parts: data.parts,
        metadata: data.metadata,
        createdAt: new Date().toISOString(),
    };
    const updated = {
        ...messages[index],
        replies: [...(messages[index].replies ?? []), reply],
    };
    messages[index] = updated;
    writeJsonFile(path, messages);
    return updated;
}
export function deleteMessage(workspaceId, channelId, messageId) {
    const path = messageFilePath(workspaceId, channelId);
    const messages = readJsonFile(path) || [];
    const index = messages.findIndex((message) => message.id === messageId);
    if (index === -1)
        return false;
    messages.splice(index, 1);
    writeJsonFile(path, messages);
    return true;
}
export function clearMessages(workspaceId, channelId) {
    const path = messageFilePath(workspaceId, channelId);
    const messages = readJsonFile(path) || [];
    if (messages.length === 0)
        return false;
    writeJsonFile(path, []);
    return true;
}
//# sourceMappingURL=message.js.map