/**
 * Model Routes Module
 * Handles model management API endpoints for cli-tools.json
 */
import { getAllConfiguredModels, getModelsForTool, addModelForTool, removeModelForTool } from '../../tools/claude-cli-tools.js';
/**
 * Handle Model Management routes
 * @returns true if route was handled, false otherwise
 */
export async function handleModelRoutes(ctx) {
    const { pathname, req, res, handlePostRequest, broadcastToClients, initialPath } = ctx;
    // ========== GET ALL MODELS ==========
    // GET /api/models
    if (pathname === '/api/models' && req.method === 'GET') {
        try {
            const models = getAllConfiguredModels(initialPath);
            res.writeHead(200, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({ models }));
        }
        catch (err) {
            res.writeHead(500, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({ error: err.message }));
        }
        return true;
    }
    // ========== GET MODELS FOR TOOL ==========
    // GET /api/models/:tool
    const getToolMatch = pathname.match(/^\/api\/models\/([^/]+)$/);
    if (getToolMatch && req.method === 'GET') {
        const toolName = decodeURIComponent(getToolMatch[1]);
        try {
            const models = getModelsForTool(initialPath, toolName);
            res.writeHead(200, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({ tool: toolName, models }));
        }
        catch (err) {
            res.writeHead(500, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({ error: err.message }));
        }
        return true;
    }
    // ========== ADD MODEL TO TOOL ==========
    // POST /api/models/:tool
    const postToolMatch = pathname.match(/^\/api\/models\/([^/]+)$/);
    if (postToolMatch && req.method === 'POST') {
        const toolName = decodeURIComponent(postToolMatch[1]);
        handlePostRequest(req, res, async (body) => {
            try {
                const { model } = body;
                // Validate required fields
                if (!model || typeof model !== 'string' || model.trim() === '') {
                    return { error: 'model is required and must be a non-empty string', status: 400 };
                }
                const config = addModelForTool(initialPath, toolName, model);
                // Broadcast update event
                broadcastToClients({
                    type: 'CLI_MODELS_UPDATED',
                    payload: {
                        tool: toolName,
                        action: 'add',
                        model,
                        timestamp: new Date().toISOString()
                    }
                });
                return {
                    success: true,
                    tool: toolName,
                    model,
                    models: config.models?.[toolName] || []
                };
            }
            catch (err) {
                return { error: err.message, status: 500 };
            }
        });
        return true;
    }
    // ========== DELETE MODEL FROM TOOL ==========
    // DELETE /api/models/:tool/:model
    const deleteMatch = pathname.match(/^\/api\/models\/([^/]+)\/([^/]+)$/);
    if (deleteMatch && req.method === 'DELETE') {
        const toolName = decodeURIComponent(deleteMatch[1]);
        const modelName = decodeURIComponent(deleteMatch[2]);
        try {
            const config = removeModelForTool(initialPath, toolName, modelName);
            // Broadcast update event
            broadcastToClients({
                type: 'CLI_MODELS_UPDATED',
                payload: {
                    tool: toolName,
                    action: 'remove',
                    model: modelName,
                    timestamp: new Date().toISOString()
                }
            });
            res.writeHead(200, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({
                success: true,
                tool: toolName,
                model: modelName,
                models: config.models?.[toolName] || []
            }));
        }
        catch (err) {
            res.writeHead(500, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({ error: err.message }));
        }
        return true;
    }
    return false;
}
//# sourceMappingURL=model-routes.js.map