"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const api = __importStar(require("../api/workflow-sets"));
const router = (0, express_1.Router)();
/**
 * POST /api/workflow-sets
 * Create a workflow set from a specification and start the planner workflow.
 */
router.post('/', async (req, res) => {
    const { name, description, specification } = req.body;
    const result = await api.createWorkflowSet({ name, description, specification }, req.auth ? { userId: req.auth.userId } : undefined);
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/workflow-sets
 * List workflow sets with optional filters.
 */
router.get('/', async (req, res) => {
    const result = await api.listWorkflowSets({
        status: req.query.status,
        search: req.query.search,
        limit: req.query.limit ? parseInt(req.query.limit, 10) : undefined,
        offset: req.query.offset ? parseInt(req.query.offset, 10) : undefined,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/workflow-sets/:id
 * Get a single workflow set with its plan and workflow statuses.
 */
router.get('/:id', async (req, res) => {
    const result = await api.getWorkflowSet({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * PUT /api/workflow-sets/:id/plan
 * Update the plan (engineer adjustments before building).
 */
router.put('/:id/plan', async (req, res) => {
    const { plan, namespaces } = req.body;
    const result = await api.updateWorkflowSetPlanApi({
        id: req.params.id,
        plan,
        namespaces,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/workflow-sets/:id/add
 * Add additional workflows to an existing set from a new specification.
 */
router.post('/:id/add', async (req, res) => {
    const { specification } = req.body;
    const result = await api.addToWorkflowSet({ id: req.params.id, specification }, req.auth ? { userId: req.auth.userId } : undefined);
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/workflow-sets/:id/build
 * Trigger the build phase (resume the planner workflow).
 */
router.post('/:id/build', async (req, res) => {
    const result = await api.buildWorkflowSet({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/workflow-sets/:id/deploy
 * Deploy all namespaces in the set.
 */
router.post('/:id/deploy', async (req, res) => {
    const result = await api.deployWorkflowSet({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
exports.default = router;
