"use strict";
/**
 * Provider Routes - OpenAI-compatible provider management
 */
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const openai_compat_manager_1 = require("../../cliproxy/ai-providers/openai-compat-manager");
const reserved_names_1 = require("../../config/reserved-names");
const router = (0, express_1.Router)();
/**
 * GET /api/cliproxy/openai-compat - List all OpenAI-compatible providers
 */
router.get('/', (_req, res) => {
    try {
        const providers = (0, openai_compat_manager_1.listOpenAICompatProviders)();
        // Mask API keys for security
        const masked = providers.map((p) => ({
            ...p,
            apiKey: p.apiKey ? (p.apiKey.length > 8 ? `...${p.apiKey.slice(-4)}` : '***') : '',
        }));
        res.json({ providers: masked });
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
/**
 * GET /api/cliproxy/openai-compat/templates - Get pre-configured provider templates
 */
router.get('/templates', (_req, res) => {
    res.json({
        templates: [
            { ...openai_compat_manager_1.OPENROUTER_TEMPLATE, description: 'OpenRouter - Access multiple AI models' },
            { ...openai_compat_manager_1.TOGETHER_TEMPLATE, description: 'Together AI - Open source models' },
        ],
    });
});
/**
 * GET /api/cliproxy/openai-compat/:name - Get a specific provider
 */
router.get('/:name', (req, res) => {
    try {
        const provider = (0, openai_compat_manager_1.getOpenAICompatProvider)(req.params.name);
        if (!provider) {
            res.status(404).json({ error: `Provider '${req.params.name}' not found` });
            return;
        }
        // Mask API key
        res.json({
            ...provider,
            apiKey: provider.apiKey
                ? provider.apiKey.length > 8
                    ? `...${provider.apiKey.slice(-4)}`
                    : '***'
                : '',
        });
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
/**
 * POST /api/cliproxy/openai-compat - Add a new provider
 * Body: { name, baseUrl, apiKey, models: [{ name, alias }] }
 */
router.post('/', (req, res) => {
    try {
        const { name, baseUrl, apiKey, models } = req.body;
        // Validation
        if (!name || typeof name !== 'string') {
            res.status(400).json({ error: 'name is required' });
            return;
        }
        // Validate reserved names
        if ((0, reserved_names_1.isReservedName)(name)) {
            res.status(400).json({
                error: `Provider name '${name}' is reserved`,
                reserved: reserved_names_1.RESERVED_PROFILE_NAMES,
            });
            return;
        }
        if (!baseUrl || typeof baseUrl !== 'string') {
            res.status(400).json({ error: 'baseUrl is required' });
            return;
        }
        if (!apiKey || typeof apiKey !== 'string') {
            res.status(400).json({ error: 'apiKey is required' });
            return;
        }
        if (models && !Array.isArray(models)) {
            res.status(400).json({ error: 'models must be an array' });
            return;
        }
        (0, openai_compat_manager_1.addOpenAICompatProvider)({
            name,
            baseUrl,
            apiKey,
            models: models || [],
        });
        res.status(201).json({ success: true, name });
    }
    catch (error) {
        const message = error.message;
        if (message.includes('already exists')) {
            res.status(409).json({ error: message });
        }
        else {
            res.status(500).json({ error: message });
        }
    }
});
/**
 * PUT /api/cliproxy/openai-compat/:name - Update a provider
 * Body: { baseUrl?, apiKey?, models?, name? (for rename) }
 */
router.put('/:name', (req, res) => {
    try {
        const { baseUrl, apiKey, models, name: newName } = req.body;
        (0, openai_compat_manager_1.updateOpenAICompatProvider)(req.params.name, {
            baseUrl,
            apiKey,
            models,
            name: newName,
        });
        res.json({ success: true });
    }
    catch (error) {
        const message = error.message;
        if (message.includes('not found')) {
            res.status(404).json({ error: message });
        }
        else {
            res.status(500).json({ error: message });
        }
    }
});
/**
 * DELETE /api/cliproxy/openai-compat/:name - Remove a provider
 */
router.delete('/:name', (req, res) => {
    try {
        const removed = (0, openai_compat_manager_1.removeOpenAICompatProvider)(req.params.name);
        if (!removed) {
            res.status(404).json({ error: `Provider '${req.params.name}' not found` });
            return;
        }
        res.json({ success: true });
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
exports.default = router;
//# sourceMappingURL=provider-routes.js.map