"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const zod_1 = require("zod");
const workflow_controller_1 = require("../controllers/workflow.controller");
const validation_middleware_1 = require("../middleware/validation.middleware");
const rateLimit_middleware_1 = require("../middleware/rateLimit.middleware");
const router = (0, express_1.Router)();
const workflowController = new workflow_controller_1.WorkflowController();
// Validation schemas
const TransitionSchema = zod_1.z.object({
    taskId: zod_1.z.string().uuid(),
    event: zod_1.z.string(),
    metadata: zod_1.z.record(zod_1.z.string(), zod_1.z.any()).optional(),
    userId: zod_1.z.string().uuid().optional(),
    reason: zod_1.z.string().optional()
});
const WebhookConfigSchema = zod_1.z.object({
    url: zod_1.z.string().url(),
    events: zod_1.z.array(zod_1.z.string()),
    headers: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(),
    secret: zod_1.z.string().optional(),
    active: zod_1.z.boolean().default(true),
    retryConfig: zod_1.z.object({
        maxRetries: zod_1.z.number().int().min(0).max(10).default(3),
        retryDelay: zod_1.z.number().int().min(1000).default(2000),
        backoffMultiplier: zod_1.z.number().min(1).max(5).default(2)
    }).optional()
});
const JobSchema = zod_1.z.object({
    type: zod_1.z.string(),
    taskId: zod_1.z.string().uuid().optional(),
    payload: zod_1.z.any(),
    metadata: zod_1.z.record(zod_1.z.string(), zod_1.z.any()).optional(),
    priority: zod_1.z.number().int().min(1).max(20).optional(),
    delay: zod_1.z.number().int().min(0).optional(),
    cron: zod_1.z.string().optional()
});
const AuditQuerySchema = zod_1.z.object({
    entityType: zod_1.z.string().optional(),
    entityId: zod_1.z.string().optional(),
    actorId: zod_1.z.string().optional(),
    action: zod_1.z.string().optional(),
    severity: zod_1.z.enum(['info', 'warning', 'error', 'critical']).optional(),
    startDate: zod_1.z.string().datetime().optional(),
    endDate: zod_1.z.string().datetime().optional(),
    tags: zod_1.z.array(zod_1.z.string()).optional(),
    limit: zod_1.z.coerce.number().int().min(1).max(1000).default(100),
    offset: zod_1.z.coerce.number().int().min(0).default(0),
    sort: zod_1.z.string().optional()
});
/**
 * State Machine Routes
 */
// Get current state for a task
router.get('/state/:taskId', rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({ taskId: zod_1.z.string().uuid() }), 'params'), (req, res) => workflowController.getTaskState(req, res));
// Get available transitions for a task
router.get('/state/:taskId/transitions', rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({ taskId: zod_1.z.string().uuid() }), 'params'), (req, res) => workflowController.getAvailableTransitions(req, res));
// Execute a state transition
router.post('/state/transition', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(TransitionSchema, 'body'), (req, res) => workflowController.executeTransition(req, res));
// Validate if a transition is allowed
router.post('/state/validate', rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validate)(TransitionSchema, 'body'), (req, res) => workflowController.validateTransition(req, res));
/**
 * Webhook Routes
 */
// Get all webhooks
router.get('/webhooks', rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({
    active: zod_1.z.coerce.boolean().optional(),
    event: zod_1.z.string().optional()
}), 'query'), (req, res) => workflowController.getWebhooks(req, res));
// Register a new webhook
router.post('/webhooks', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(WebhookConfigSchema, 'body'), (req, res) => workflowController.registerWebhook(req, res));
// Update webhook configuration
router.put('/webhooks/:id', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validateMultiple)({
    params: zod_1.z.object({ id: zod_1.z.string().uuid() }),
    body: WebhookConfigSchema.partial()
}), (req, res) => workflowController.updateWebhook(req, res));
// Delete a webhook
router.delete('/webhooks/:id', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({ id: zod_1.z.string().uuid() }), 'params'), (req, res) => workflowController.deleteWebhook(req, res));
// Test webhook endpoint
router.post('/webhooks/:id/test', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({ id: zod_1.z.string().uuid() }), 'params'), (req, res) => workflowController.testWebhook(req, res));
// Get webhook delivery history
router.get('/webhooks/:id/deliveries', rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validateMultiple)({
    params: zod_1.z.object({ id: zod_1.z.string().uuid() }),
    query: zod_1.z.object({ limit: zod_1.z.coerce.number().int().min(1).max(100).default(100) })
}), (req, res) => workflowController.getWebhookDeliveries(req, res));
/**
 * Audit Log Routes
 */
// Query audit logs
router.get('/audit', rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validate)(AuditQuerySchema, 'query'), (req, res) => workflowController.queryAuditLogs(req, res));
// Get audit statistics
router.get('/audit/statistics', rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({
    startDate: zod_1.z.string().datetime().optional(),
    endDate: zod_1.z.string().datetime().optional()
}), 'query'), (req, res) => workflowController.getAuditStatistics(req, res));
// Get audit timeline for an entity
router.get('/audit/timeline/:entityId', rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validateMultiple)({
    params: zod_1.z.object({ entityId: zod_1.z.string() }),
    query: zod_1.z.object({ entityType: zod_1.z.string().optional() })
}), (req, res) => workflowController.getAuditTimeline(req, res));
// Export audit logs
router.get('/audit/export', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(AuditQuerySchema.extend({
    format: zod_1.z.enum(['json', 'csv']).default('json')
}), 'query'), (req, res) => workflowController.exportAuditLogs(req, res));
/**
 * Job Queue Routes
 */
// Add a job to queue
router.post('/jobs', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(JobSchema.extend({
    queue: zod_1.z.string().default('workflow')
}), 'body'), (req, res) => workflowController.addJob(req, res));
// Schedule a job
router.post('/jobs/schedule', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(JobSchema.extend({
    queue: zod_1.z.string().default('scheduled'),
    delay: zod_1.z.number().int().min(0).optional(),
    scheduleAt: zod_1.z.string().datetime().optional()
}), 'body'), (req, res) => workflowController.scheduleJob(req, res));
// Add recurring job
router.post('/jobs/recurring', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(JobSchema.extend({
    queue: zod_1.z.string().default('scheduled'),
    cron: zod_1.z.string()
}), 'body'), (req, res) => workflowController.addRecurringJob(req, res));
// Get queue statistics
router.get('/jobs/queues/:name/stats', rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({ name: zod_1.z.string() }), 'params'), (req, res) => workflowController.getQueueStatistics(req, res));
// Pause a queue
router.post('/jobs/queues/:name/pause', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({ name: zod_1.z.string() }), 'params'), (req, res) => workflowController.pauseQueue(req, res));
// Resume a queue
router.post('/jobs/queues/:name/resume', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({ name: zod_1.z.string() }), 'params'), (req, res) => workflowController.resumeQueue(req, res));
// Clear a queue
router.delete('/jobs/queues/:name/clear', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({ name: zod_1.z.string() }), 'params'), (req, res) => workflowController.clearQueue(req, res));
/**
 * Git Integration Routes
 */
// Create branch for task
router.post('/git/branch', rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({
    taskId: zod_1.z.string().uuid(),
    taskTitle: zod_1.z.string()
}), 'body'), (req, res) => workflowController.createTaskBranch(req, res));
// Get Git info for task
router.get('/git/task/:taskId', rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({ taskId: zod_1.z.string().uuid() }), 'params'), (req, res) => workflowController.getTaskGitInfo(req, res));
// Handle GitHub webhook events
router.post('/git/webhook', rateLimit_middleware_1.strictLimiter, (req, res) => workflowController.handleGitWebhook(req, res));
exports.default = router;
//# sourceMappingURL=workflow.routes.js.map