import { requireAuth } from '../middleware/auth.js';
import { readRateLimit, writeRateLimit } from '../middleware/rateLimit.js';
import { getPrismaClient } from '../db/prisma.js';
const MAX_SERVER_CHATS = 5;
export async function chatRoutes(fastify) {
    function getClientId(request) {
        return request.headers['x-client-id'] || 'default-client';
    }
    fastify.post('/api/chats', {
        preHandler: [requireAuth, writeRateLimit],
    }, async (request, reply) => {
        const body = request.body;
        const clientId = getClientId(request);
        const logger = request.logger;
        try {
            const prisma = await getPrismaClient();
            const existingChats = await prisma.chatHistory.findMany({
                where: { clientId },
                orderBy: { lastMessageAt: 'desc' },
            });
            if (existingChats.length >= MAX_SERVER_CHATS) {
                const oldestChat = existingChats[existingChats.length - 1];
                await prisma.chatHistory.delete({
                    where: { id: oldestChat.id },
                });
                logger?.info('Removed oldest chat to maintain limit', { clientId, removedId: oldestChat.id });
            }
            const chatData = {
                clientId,
                agentName: body.agentName,
                agentDescription: body.agentDescription || null,
                messages: body.messages || [],
                lastMessageAt: new Date(body.lastMessageAt || Date.now()),
            };
            let chat;
            if (body.id) {
                try {
                    chat = await prisma.chatHistory.update({
                        where: { id: body.id },
                        data: chatData,
                    });
                }
                catch {
                    chat = await prisma.chatHistory.create({
                        data: chatData,
                    });
                }
            }
            else {
                chat = await prisma.chatHistory.create({
                    data: chatData,
                });
            }
            logger?.info('Chat saved to server', { chatId: chat.id, clientId });
            reply.status(201);
            return {
                success: true,
                chat: {
                    id: chat.id,
                    agentName: chat.agentName,
                    agentDescription: chat.agentDescription,
                    messages: chat.messages,
                    createdAt: chat.createdAt,
                    lastMessageAt: chat.lastMessageAt,
                },
            };
        }
        catch (error) {
            logger?.error('Failed to save chat', { error: error.message });
            return reply.status(500).send({
                error: 'Failed to save chat',
                message: error.message,
            });
        }
    });
    fastify.get('/api/chats', {
        preHandler: [requireAuth, readRateLimit],
    }, async (request, reply) => {
        const clientId = getClientId(request);
        const logger = request.logger;
        try {
            const prisma = await getPrismaClient();
            const chats = await prisma.chatHistory.findMany({
                where: { clientId },
                orderBy: { lastMessageAt: 'desc' },
                take: MAX_SERVER_CHATS,
            });
            return {
                success: true,
                chats: chats.map(chat => ({
                    id: chat.id,
                    agentName: chat.agentName,
                    agentDescription: chat.agentDescription,
                    messages: chat.messages,
                    createdAt: chat.createdAt,
                    lastMessageAt: chat.lastMessageAt,
                })),
                count: chats.length,
                max: MAX_SERVER_CHATS,
            };
        }
        catch (error) {
            logger?.error('Failed to list chats', { error: error.message });
            return reply.status(500).send({
                error: 'Failed to list chats',
                message: error.message,
            });
        }
    });
    fastify.get('/api/chats/:id', {
        preHandler: [requireAuth, readRateLimit],
    }, async (request, reply) => {
        const { id } = request.params;
        const clientId = getClientId(request);
        const logger = request.logger;
        try {
            const prisma = await getPrismaClient();
            const chat = await prisma.chatHistory.findFirst({
                where: {
                    id,
                    clientId,
                },
            });
            if (!chat) {
                return reply.status(404).send({
                    error: 'Chat not found',
                });
            }
            return {
                success: true,
                chat: {
                    id: chat.id,
                    agentName: chat.agentName,
                    agentDescription: chat.agentDescription,
                    messages: chat.messages,
                    createdAt: chat.createdAt,
                    lastMessageAt: chat.lastMessageAt,
                },
            };
        }
        catch (error) {
            logger?.error('Failed to get chat', { error: error.message });
            return reply.status(500).send({
                error: 'Failed to get chat',
                message: error.message,
            });
        }
    });
}
//# sourceMappingURL=chats.js.map