"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CollaborationController = void 0;
const collaborationService_1 = require("./collaborationService");
/**
 * Collaboration Controller
 * Handles HTTP requests for collaboration features
 */
class CollaborationController {
    /**
     * Get collaboration status for a document
     */
    async getDocumentStatus(req, res) {
        try {
            const { documentId } = req.params;
            const stats = collaborationService_1.collaborationService.getDocumentStats(documentId);
            const activeUsers = collaborationService_1.collaborationService.getActiveUsers(documentId);
            res.json({
                success: true,
                data: {
                    documentId,
                    ...stats,
                    activeUsers
                }
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Get change history for a document
     */
    async getChangeHistory(req, res) {
        try {
            const { documentId } = req.params;
            const limit = req.query.limit ? parseInt(req.query.limit) : 100;
            const history = collaborationService_1.collaborationService.getChangeHistory(documentId, limit);
            res.json({
                success: true,
                data: {
                    documentId,
                    history,
                    count: history.length
                }
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Create a snapshot of the current document state
     */
    async createSnapshot(req, res) {
        try {
            const { documentId } = req.params;
            const snapshot = collaborationService_1.collaborationService.createSnapshot(documentId);
            if (!snapshot) {
                res.status(404).json({
                    success: false,
                    error: 'Document not found'
                });
                return;
            }
            res.json({
                success: true,
                data: {
                    documentId,
                    snapshot,
                    timestamp: new Date().toISOString()
                }
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Restore document from a snapshot
     */
    async restoreSnapshot(req, res) {
        try {
            const { documentId } = req.params;
            const { snapshot } = req.body;
            const userId = req.userId || 'api';
            if (!snapshot) {
                res.status(400).json({
                    success: false,
                    error: 'Snapshot data is required'
                });
                return;
            }
            collaborationService_1.collaborationService.restoreFromSnapshot(documentId, snapshot, userId);
            res.json({
                success: true,
                message: 'Document restored from snapshot'
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Apply external changes to a document
     */
    async applyChange(req, res) {
        try {
            const { documentId } = req.params;
            const { field, value } = req.body;
            const userId = req.userId || 'api';
            if (!field || value === undefined) {
                res.status(400).json({
                    success: false,
                    error: 'Field and value are required'
                });
                return;
            }
            collaborationService_1.collaborationService.applyExternalChange(documentId, field, value, userId);
            res.json({
                success: true,
                message: 'Change applied successfully'
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Get active users for a document
     */
    async getActiveUsers(req, res) {
        try {
            const { documentId } = req.params;
            const users = collaborationService_1.collaborationService.getActiveUsers(documentId);
            res.json({
                success: true,
                data: {
                    documentId,
                    users,
                    count: users.length
                }
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Clean up a document (admin only)
     */
    async cleanupDocument(req, res) {
        try {
            const { documentId } = req.params;
            collaborationService_1.collaborationService.cleanupDocument(documentId);
            res.json({
                success: true,
                message: `Document ${documentId} cleaned up successfully`
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message
            });
        }
    }
}
exports.CollaborationController = CollaborationController;
//# sourceMappingURL=collaborationController.js.map