import { Router } from 'express';
import * as agentService from '../services/agent.js';
import { generateAgentDesign, optimizeAgentPrompt } from '../agents/agent-designer.js';
const router = Router({ mergeParams: true });
router.get('/usage/dashboard', (req, res) => {
    const days = Number(req.query.days ?? 30);
    res.json(agentService.usageDashboard(Number.isFinite(days) ? days : 30));
});
router.get('/presets', (req, res) => {
    res.json(agentService.listTemplates());
});
router.get('/presets/:presetId', (req, res) => {
    const preset = agentService.readAgentTemplate(req.params.presetId);
    if (!preset) {
        res.status(404).json({ error: 'agent preset not found' });
        return;
    }
    res.json(preset);
});
router.post('/presets', (req, res) => {
    const body = req.body;
    const preset = agentService.createPreset(req.params.id, body);
    res.status(201).json(preset);
});
router.post('/presets/sync-workspaces', (_req, res) => {
    res.json(agentService.syncTemplatesToAllWorkspaces());
});
router.post('/presets/generate', async (req, res) => {
    const { prompt } = req.body;
    if (!prompt?.trim()) {
        res.status(400).json({ error: 'prompt is required' });
        return;
    }
    try {
        res.json(await generateAgentDesign(prompt));
    }
    catch (err) {
        console.error('[agent-designer] generate failed', err);
        res.status(400).json({ error: err instanceof Error ? err.message : 'agent generation failed' });
    }
});
router.post('/presets/optimize-prompt', async (req, res) => {
    const { prompt, currentPrompt } = req.body;
    if (!prompt?.trim()) {
        res.status(400).json({ error: 'prompt is required' });
        return;
    }
    try {
        res.json(await optimizeAgentPrompt(prompt, currentPrompt ?? ''));
    }
    catch (err) {
        console.error('[agent-designer] optimize failed', err);
        res.status(400).json({ error: err instanceof Error ? err.message : 'prompt optimization failed' });
    }
});
router.post('/presets/test-connection', async (req, res) => {
    const workspaceId = req.params.id;
    const data = req.body;
    if (workspaceId) {
        const result = await agentService.testConnection(workspaceId, data);
        if (!result) {
            res.status(404).json({ error: 'workspace not found' });
            return;
        }
        res.status(result.success ? 200 : 400).json(result);
        return;
    }
    // Global: test connection without workspace context
    const { apiBase, apiKey, modelId, modelProvider } = data;
    const result = await agentService.testConnection('', { apiBase, apiKey, modelId, modelProvider });
    if (!result) {
        res.status(400).json({ error: 'connection test failed' });
        return;
    }
    res.status(result.success ? 200 : 400).json(result);
});
router.put('/presets/:presetId', (req, res) => {
    const data = req.body;
    const preset = agentService.updatePreset(req.params.id, req.params.presetId, data);
    if (!preset) {
        res.status(404).json({ error: 'agent preset not found' });
        return;
    }
    res.json(preset);
});
router.delete('/presets/:presetId', (req, res) => {
    const workspaceId = req.params.id;
    if (workspaceId) {
        const deleted = agentService.deletePreset(workspaceId, req.params.presetId);
        if (deleted === null) {
            res.status(404).json({ error: 'workspace not found' });
            return;
        }
        if (!deleted) {
            res.status(404).json({ error: 'agent preset not found' });
            return;
        }
        res.status(204).end();
        return;
    }
    const deleted = agentService.deleteGlobalPreset(req.params.presetId);
    if (!deleted) {
        res.status(404).json({ error: 'agent preset not found' });
        return;
    }
    res.status(204).end();
});
router.get('/', (req, res) => {
    const sessions = agentService.list(req.params.id);
    res.json(sessions);
});
router.post('/start', (req, res) => {
    const { role, issueId } = req.body;
    if (!role) {
        res.status(400).json({ error: 'role is required' });
        return;
    }
    if (!agentService.isValidRole(role)) {
        res.status(400).json({ error: `invalid role: ${role}` });
        return;
    }
    const session = agentService.create(req.params.id, role);
    res.status(201).json(session);
});
router.post('/:agentId/stop', (req, res) => {
    const session = agentService.complete(req.params.id, req.params.agentId);
    if (!session) {
        res.status(404).json({ error: 'agent session not found' });
        return;
    }
    res.json(session);
});
router.get('/:agentId', (req, res) => {
    const session = agentService.getById(req.params.id, req.params.agentId);
    if (!session) {
        res.status(404).json({ error: 'agent session not found' });
        return;
    }
    res.json(session);
});
export default router;
//# sourceMappingURL=agent.js.map