import { randomBytes } from 'crypto';
export function generateId() {
    const now = new Date().toISOString();
    const rand = randomBytes(6).toString('hex');
    return `${now}-${rand}`;
}
export function createConversation(db, squadName) {
    const id = generateId();
    const createdAt = new Date().toISOString();
    db.prepare('INSERT INTO conversations (id, squad_name, created_at) VALUES (?, ?, ?)').run(id, squadName, createdAt);
    return { id, squad_name: squadName, created_at: createdAt };
}
export function getActiveConversation(db, squadName) {
    const row = db.prepare('SELECT * FROM conversations WHERE squad_name = ? ORDER BY id DESC LIMIT 1').get(squadName);
    return row ?? null;
}
export function getOrCreateConversation(db, squadName) {
    const existing = getActiveConversation(db, squadName);
    if (existing) {
        return { conversation: existing, created: false };
    }
    const conversation = createConversation(db, squadName);
    return { conversation, created: true };
}
export function insertMessage(db, params) {
    const id = generateId();
    const createdAt = new Date().toISOString();
    const agentId = params.agentId ?? null;
    const agentName = params.agentName ?? null;
    db.prepare('INSERT INTO chat_messages (id, conversation_id, role, content, agent_id, agent_name, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)').run(id, params.conversationId, params.role, params.content, agentId, agentName, createdAt);
    return {
        id,
        conversation_id: params.conversationId,
        role: params.role,
        content: params.content,
        agent_id: agentId,
        agent_name: agentName,
        created_at: createdAt,
    };
}
export function getMessages(db, conversationId, opts) {
    const limit = opts.limit;
    let rows;
    if (opts.cursor) {
        rows = db.prepare('SELECT * FROM chat_messages WHERE conversation_id = ? AND id > ? ORDER BY id ASC LIMIT ?').all(conversationId, opts.cursor, limit + 1);
    }
    else {
        rows = db.prepare('SELECT * FROM chat_messages WHERE conversation_id = ? ORDER BY id ASC LIMIT ?').all(conversationId, limit + 1);
    }
    const hasMore = rows.length > limit;
    const data = hasMore ? rows.slice(0, limit) : rows;
    const nextCursor = hasMore ? data[data.length - 1].id : null;
    return { data, nextCursor };
}
export function getConversations(db, squadName, opts) {
    const limit = opts.limit;
    let rows;
    if (opts.cursor) {
        rows = db.prepare('SELECT * FROM conversations WHERE squad_name = ? AND id < ? ORDER BY id DESC LIMIT ?').all(squadName, opts.cursor, limit + 1);
    }
    else {
        rows = db.prepare('SELECT * FROM conversations WHERE squad_name = ? ORDER BY id DESC LIMIT ?').all(squadName, limit + 1);
    }
    const hasMore = rows.length > limit;
    const data = hasMore ? rows.slice(0, limit) : rows;
    const nextCursor = hasMore ? data[data.length - 1].id : null;
    return { data, nextCursor };
}
export function deleteConversation(db, conversationId) {
    db.prepare('DELETE FROM chat_messages WHERE conversation_id = ?').run(conversationId);
    const result = db.prepare('DELETE FROM conversations WHERE id = ?').run(conversationId);
    return result.changes > 0;
}
export function getRecentMessages(db, conversationId, count) {
    const rows = db.prepare('SELECT * FROM chat_messages WHERE conversation_id = ? ORDER BY id DESC LIMIT ?').all(conversationId, count);
    return rows.reverse();
}
//# sourceMappingURL=conversation.js.map