"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const zod_1 = require("zod");
const collaborationController_1 = require("./collaborationController");
const validation_middleware_1 = require("../api/middleware/validation.middleware");
const rbac_middleware_1 = require("../api/middleware/rbac.middleware");
const rateLimit_middleware_1 = require("../api/middleware/rateLimit.middleware");
const rbac_service_1 = require("../api/services/rbac.service");
const router = (0, express_1.Router)();
const collaborationController = new collaborationController_1.CollaborationController();
// Validation schemas
const DocumentIdSchema = zod_1.z.object({
    documentId: zod_1.z.string().min(1)
});
const ChangeHistorySchema = zod_1.z.object({
    limit: zod_1.z.coerce.number().int().min(1).max(1000).optional()
});
const RestoreSnapshotSchema = zod_1.z.object({
    snapshot: zod_1.z.record(zod_1.z.any())
});
const ApplyChangeSchema = zod_1.z.object({
    field: zod_1.z.string().min(1),
    value: zod_1.z.any()
});
/**
 * @route   GET /api/collaboration/:documentId/status
 * @desc    Get collaboration status for a document
 * @access  Protected
 */
router.get('/:documentId/status', rbac_middleware_1.authenticateToken, rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validate)(DocumentIdSchema, 'params'), (req, res) => collaborationController.getDocumentStatus(req, res));
/**
 * @route   GET /api/collaboration/:documentId/history
 * @desc    Get change history for a document
 * @access  Protected
 */
router.get('/:documentId/history', rbac_middleware_1.authenticateToken, rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validateMultiple)({
    params: DocumentIdSchema,
    query: ChangeHistorySchema
}), (req, res) => collaborationController.getChangeHistory(req, res));
/**
 * @route   GET /api/collaboration/:documentId/snapshot
 * @desc    Create a snapshot of the current document state
 * @access  Protected
 */
router.get('/:documentId/snapshot', rbac_middleware_1.authenticateToken, rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validate)(DocumentIdSchema, 'params'), (req, res) => collaborationController.createSnapshot(req, res));
/**
 * @route   POST /api/collaboration/:documentId/restore
 * @desc    Restore document from a snapshot
 * @access  Protected - Requires UPDATE_TASK permission
 */
router.post('/:documentId/restore', rbac_middleware_1.authenticateToken, (0, rbac_middleware_1.requirePermission)(rbac_service_1.Permission.UPDATE_TASK), rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validateMultiple)({
    params: DocumentIdSchema,
    body: RestoreSnapshotSchema
}), (req, res) => collaborationController.restoreSnapshot(req, res));
/**
 * @route   POST /api/collaboration/:documentId/apply
 * @desc    Apply external changes to a document
 * @access  Protected - Requires UPDATE_TASK permission
 */
router.post('/:documentId/apply', rbac_middleware_1.authenticateToken, (0, rbac_middleware_1.requirePermission)(rbac_service_1.Permission.UPDATE_TASK), rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validateMultiple)({
    params: DocumentIdSchema,
    body: ApplyChangeSchema
}), (req, res) => collaborationController.applyChange(req, res));
/**
 * @route   GET /api/collaboration/:documentId/users
 * @desc    Get active users for a document
 * @access  Protected
 */
router.get('/:documentId/users', rbac_middleware_1.authenticateToken, rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validate)(DocumentIdSchema, 'params'), (req, res) => collaborationController.getActiveUsers(req, res));
/**
 * @route   DELETE /api/collaboration/:documentId
 * @desc    Clean up a document (admin only)
 * @access  Protected - Admin only
 */
router.delete('/:documentId', rbac_middleware_1.authenticateToken, (0, rbac_middleware_1.requirePermission)(rbac_service_1.Permission.MANAGE_SYSTEM), rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(DocumentIdSchema, 'params'), (req, res) => collaborationController.cleanupDocument(req, res));
exports.default = router;
//# sourceMappingURL=collaborationRoutes.js.map