import { Router } from 'express';
import * as ws from '../services/workflow.js';
const router = Router();
// ---- Workflow CRUD ----
router.get('/', (_req, res) => {
    try {
        const folderId = _req.query.folderId;
        const workflows = ws.listWorkflows(folderId === 'null' ? null : folderId);
        res.json(workflows);
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
// Static routes must be declared before /:workflowId.
router.get('/folders', (_req, res) => {
    try {
        res.json(ws.listFolders());
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.post('/folders', (req, res) => {
    try {
        const folder = ws.createFolder(req.body);
        res.status(201).json(folder);
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.put('/folders/:folderId', (req, res) => {
    try {
        ws.updateFolder(req.params.folderId, req.body);
        const folders = ws.listFolders();
        res.json(folders.find(f => f.id === req.params.folderId));
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.delete('/folders/:folderId', (req, res) => {
    try {
        ws.deleteFolder(req.params.folderId);
        res.status(204).send();
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.get('/execution-logs/all', (req, res) => {
    try {
        const limit = req.query.limit ? parseInt(req.query.limit, 10) : 50;
        res.json(ws.listAllExecutionLogs(limit));
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.post('/validate-cron', (req, res) => {
    try {
        const { cron } = req.body;
        if (typeof cron !== 'string') {
            res.status(400).json({ error: 'cron field is required' });
            return;
        }
        res.json(ws.validateCron(cron));
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.get('/:workflowId', (req, res) => {
    try {
        const workflow = ws.getWorkflow(req.params.workflowId);
        if (!workflow) {
            res.status(404).json({ error: 'Workflow not found' });
            return;
        }
        res.json(workflow);
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.post('/', (req, res) => {
    try {
        const workflow = ws.createWorkflow(req.body);
        res.status(201).json(workflow);
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.put('/:workflowId', (req, res) => {
    try {
        const workflow = ws.updateWorkflow(req.params.workflowId, req.body);
        res.json(workflow);
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.delete('/:workflowId', (req, res) => {
    try {
        ws.deleteWorkflow(req.params.workflowId);
        res.status(204).send();
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.post('/:workflowId/duplicate', (req, res) => {
    try {
        const workflow = ws.duplicateWorkflow(req.params.workflowId);
        res.status(201).json(workflow);
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
// ---- Versions ----
router.get('/:workflowId/versions', (req, res) => {
    try {
        res.json(ws.listVersions(req.params.workflowId));
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.post('/:workflowId/versions', (req, res) => {
    try {
        const version = ws.createVersion(req.params.workflowId, req.body);
        res.status(201).json(version);
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.get('/:workflowId/versions/:versionId', (req, res) => {
    try {
        const version = ws.getVersion(req.params.workflowId, req.params.versionId);
        if (!version) {
            res.status(404).json({ error: 'Version not found' });
            return;
        }
        res.json(version);
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.delete('/:workflowId/versions/:versionId', (req, res) => {
    try {
        ws.deleteVersion(req.params.workflowId, req.params.versionId);
        res.status(204).send();
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.delete('/:workflowId/versions', (req, res) => {
    try {
        ws.clearVersions(req.params.workflowId);
        res.status(204).send();
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
// ---- Per-Workflow Execution Logs ----
router.get('/:workflowId/execution-logs', (req, res) => {
    try {
        res.json(ws.listExecutionLogs(req.params.workflowId));
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.get('/:workflowId/execution-logs/:logId', (req, res) => {
    try {
        const log = ws.getExecutionLog(req.params.workflowId, req.params.logId);
        if (!log) {
            res.status(404).json({ error: 'Execution log not found' });
            return;
        }
        res.json(log);
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.get('/:workflowId/execution-logs/:logId/path', (req, res) => {
    try {
        const filePath = ws.getExecutionLogPath(req.params.workflowId, req.params.logId);
        res.json({ path: filePath });
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.delete('/:workflowId/execution-logs/:logId', (req, res) => {
    try {
        ws.deleteExecutionLog(req.params.workflowId, req.params.logId);
        res.status(204).send();
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.delete('/:workflowId/execution-logs', (req, res) => {
    try {
        ws.clearExecutionLogs(req.params.workflowId);
        res.status(204).send();
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
// ---- Staging ----
router.get('/:workflowId/staging', (req, res) => {
    try {
        res.json(ws.loadStaging(req.params.workflowId));
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.put('/:workflowId/staging', (req, res) => {
    try {
        ws.saveStaging(req.params.workflowId, req.body);
        res.json({ ok: true });
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.delete('/:workflowId/staging', (req, res) => {
    try {
        ws.clearStaging(req.params.workflowId);
        res.status(204).send();
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
// ---- Operation History ----
router.get('/:workflowId/operation-history', (req, res) => {
    try {
        res.json(ws.loadOperationHistory(req.params.workflowId));
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.put('/:workflowId/operation-history', (req, res) => {
    try {
        const entries = Array.isArray(req.body) ? req.body : req.body?.entries;
        ws.saveOperationHistory(req.params.workflowId, Array.isArray(entries) ? entries : []);
        res.json({ ok: true });
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.delete('/:workflowId/operation-history', (req, res) => {
    try {
        ws.clearOperationHistory(req.params.workflowId);
        res.status(204).send();
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
// ---- Workflow Agent Chat ----
router.get('/:workflowId/chat', (req, res) => {
    try {
        res.json(ws.loadChat(req.params.workflowId));
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.put('/:workflowId/chat', (req, res) => {
    try {
        ws.saveChat(req.params.workflowId, Array.isArray(req.body?.messages) ? req.body.messages : []);
        res.json({ ok: true });
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.delete('/:workflowId/chat', (req, res) => {
    try {
        ws.clearChat(req.params.workflowId);
        res.status(204).send();
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
// ---- Plugin Config Schemes ----
router.get('/:workflowId/plugin-schemes/:pluginId', (req, res) => {
    try {
        res.json(ws.listPluginSchemes(req.params.workflowId, req.params.pluginId));
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.post('/:workflowId/plugin-schemes/:pluginId/:schemeName', (req, res) => {
    try {
        ws.createPluginScheme(req.params.workflowId, req.params.pluginId, req.params.schemeName);
        res.status(201).json({ ok: true });
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.get('/:workflowId/plugin-schemes/:pluginId/:schemeName', (req, res) => {
    try {
        res.json(ws.readPluginScheme(req.params.workflowId, req.params.pluginId, req.params.schemeName));
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.put('/:workflowId/plugin-schemes/:pluginId/:schemeName', (req, res) => {
    try {
        ws.savePluginScheme(req.params.workflowId, req.params.pluginId, req.params.schemeName, req.body || {});
        res.json({ ok: true });
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.delete('/:workflowId/plugin-schemes/:pluginId/:schemeName', (req, res) => {
    try {
        ws.deletePluginScheme(req.params.workflowId, req.params.pluginId, req.params.schemeName);
        res.status(204).send();
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
export default router;
//# sourceMappingURL=workflow.js.map