import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export function registerToolRoutes(fastify) {
    fastify.get('/devkit/api/tools', async (request, reply) => {
        try {
            const tools = await prisma.devKitTool.findMany({
                include: {
                    agents: {
                        include: {
                            agent: true
                        }
                    }
                },
                orderBy: {
                    createdAt: 'desc'
                }
            });
            const toolsWithCount = tools.map(tool => ({
                ...tool,
                agentsCount: tool.agents.length,
                agents: tool.agents.map(at => at.agent)
            }));
            return {
                tools: toolsWithCount,
                total: tools.length
            };
        }
        catch (error) {
            return reply.status(500).send({
                error: 'failed_to_list_tools',
                message: error.message
            });
        }
    });
    fastify.post('/devkit/api/tools', async (request, reply) => {
        try {
            const body = request.body;
            if (!body.name || !body.kind) {
                return reply.status(400).send({
                    error: 'validation_error',
                    message: 'Missing required fields: name, kind'
                });
            }
            const tool = await prisma.devKitTool.create({
                data: {
                    name: body.name,
                    kind: body.kind,
                    config: body.config || {},
                    enabled: body.enabled !== undefined ? body.enabled : true
                },
                include: {
                    agents: {
                        include: {
                            agent: true
                        }
                    }
                }
            });
            return {
                tool: {
                    ...tool,
                    agentsCount: tool.agents.length,
                    agents: tool.agents.map(at => at.agent)
                }
            };
        }
        catch (error) {
            return reply.status(500).send({
                error: 'failed_to_create_tool',
                message: error.message
            });
        }
    });
    fastify.get('/devkit/api/tools/:id', async (request, reply) => {
        try {
            const { id } = request.params;
            const tool = await prisma.devKitTool.findUnique({
                where: { id },
                include: {
                    agents: {
                        include: {
                            agent: true
                        }
                    }
                }
            });
            if (!tool) {
                return reply.status(404).send({
                    error: 'tool_not_found',
                    message: `Tool with id '${id}' not found`
                });
            }
            return {
                tool: {
                    ...tool,
                    agentsCount: tool.agents.length,
                    agents: tool.agents.map(at => ({
                        ...at.agent,
                        configOverride: at.configOverride
                    }))
                }
            };
        }
        catch (error) {
            return reply.status(500).send({
                error: 'failed_to_get_tool',
                message: error.message
            });
        }
    });
    fastify.put('/devkit/api/tools/:id', async (request, reply) => {
        try {
            const { id } = request.params;
            const body = request.body;
            const existing = await prisma.devKitTool.findUnique({
                where: { id }
            });
            if (!existing) {
                return reply.status(404).send({
                    error: 'tool_not_found',
                    message: `Tool with id '${id}' not found`
                });
            }
            const tool = await prisma.devKitTool.update({
                where: { id },
                data: {
                    ...(body.name && { name: body.name }),
                    ...(body.kind && { kind: body.kind }),
                    ...(body.config && { config: body.config }),
                    ...(body.enabled !== undefined && { enabled: body.enabled })
                },
                include: {
                    agents: {
                        include: {
                            agent: true
                        }
                    }
                }
            });
            return {
                tool: {
                    ...tool,
                    agentsCount: tool.agents.length,
                    agents: tool.agents.map(at => at.agent)
                }
            };
        }
        catch (error) {
            return reply.status(500).send({
                error: 'failed_to_update_tool',
                message: error.message
            });
        }
    });
    fastify.delete('/devkit/api/tools/:id', async (request, reply) => {
        try {
            const { id } = request.params;
            const existing = await prisma.devKitTool.findUnique({
                where: { id }
            });
            if (!existing) {
                return reply.status(404).send({
                    error: 'tool_not_found',
                    message: `Tool with id '${id}' not found`
                });
            }
            await prisma.devKitTool.delete({
                where: { id }
            });
            return {
                success: true,
                message: 'Tool deleted successfully'
            };
        }
        catch (error) {
            return reply.status(500).send({
                error: 'failed_to_delete_tool',
                message: error.message
            });
        }
    });
}
//# sourceMappingURL=tools-api.js.map