"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AssignmentController = void 0;
const assignment_service_1 = require("../services/assignment.service");
const rbac_service_1 = require("../services/rbac.service");
/**
 * Assignment Controller
 * Handles all assignment-related HTTP requests
 */
class AssignmentController {
    /**
     * Assign a task to a user
     */
    async assignTask(req, res) {
        try {
            const { taskId, assigneeId, dueDate, workloadPoints, notes } = req.body;
            const assignerId = req.userId;
            const assignment = await assignment_service_1.assignmentService.assignTask(taskId, assigneeId, assignerId, { dueDate, workloadPoints, notes });
            res.status(201).json({
                success: true,
                data: assignment
            });
        }
        catch (error) {
            res.status(error.message.includes('Permission') ? 403 : 400).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Reassign a task to a different user
     */
    async reassignTask(req, res) {
        try {
            const { taskId, newAssigneeId, reason } = req.body;
            const reassignerId = req.userId;
            const assignment = await assignment_service_1.assignmentService.reassignTask(taskId, newAssigneeId, reassignerId, reason);
            res.json({
                success: true,
                data: assignment
            });
        }
        catch (error) {
            res.status(error.message.includes('Permission') ? 403 : 400).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Unassign a task
     */
    async unassignTask(req, res) {
        try {
            const { taskId } = req.params;
            const unassignerId = req.userId;
            await assignment_service_1.assignmentService.unassignTask(taskId, unassignerId);
            res.json({
                success: true,
                message: 'Task unassigned successfully'
            });
        }
        catch (error) {
            res.status(error.message.includes('Permission') ? 403 : 400).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Get tasks assigned to a user
     */
    async getAssignedTasks(req, res) {
        try {
            const { userId } = req.params;
            const requesterId = req.userId;
            // Check if user can view other user's tasks
            if (userId !== requesterId) {
                const canViewUsers = await rbac_service_1.rbacService.hasPermission(requesterId, rbac_service_1.Permission.VIEW_USERS);
                if (!canViewUsers) {
                    res.status(403).json({
                        success: false,
                        error: 'Permission denied: Cannot view other users\' tasks'
                    });
                    return;
                }
            }
            const options = {
                includeCompleted: req.query.includeCompleted === 'true',
                projectId: req.query.projectId,
                status: req.query.status,
                sort: req.query.sort,
                limit: req.query.limit ? parseInt(req.query.limit) : undefined
            };
            const tasks = await assignment_service_1.assignmentService.getAssignedTasks(userId, options);
            res.json({
                success: true,
                data: tasks
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Get workload summary for a user
     */
    async getWorkloadSummary(req, res) {
        try {
            const { userId } = req.params;
            const requesterId = req.userId;
            // Check if user can view other user's workload
            if (userId !== requesterId) {
                const canViewUsers = await rbac_service_1.rbacService.hasPermission(requesterId, rbac_service_1.Permission.VIEW_USERS);
                if (!canViewUsers) {
                    res.status(403).json({
                        success: false,
                        error: 'Permission denied: Cannot view other users\' workload'
                    });
                    return;
                }
            }
            const summary = await assignment_service_1.assignmentService.getWorkloadSummary(userId);
            res.json({
                success: true,
                data: summary
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Get assignment recommendations for a task
     */
    async getAssignmentRecommendations(req, res) {
        try {
            const { taskId } = req.params;
            const options = {
                maxRecommendations: req.query.maxRecommendations
                    ? parseInt(req.query.maxRecommendations)
                    : undefined,
                projectTeamOnly: req.query.projectTeamOnly === 'true'
            };
            const recommendations = await assignment_service_1.assignmentService.getAssignmentRecommendations(taskId, options);
            res.json({
                success: true,
                data: recommendations
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Batch assign multiple tasks
     */
    async batchAssignTasks(req, res) {
        try {
            const { assignments } = req.body;
            const assignerId = req.userId;
            const results = await assignment_service_1.assignmentService.batchAssignTasks(assignments, assignerId);
            res.json({
                success: true,
                data: {
                    assigned: results.length,
                    assignments: results
                }
            });
        }
        catch (error) {
            res.status(400).json({
                success: false,
                error: error.message
            });
        }
    }
    /**
     * Auto-balance workload across project team
     */
    async autoBalanceWorkload(req, res) {
        try {
            const { projectId } = req.params;
            const balancerId = req.userId;
            const result = await assignment_service_1.assignmentService.autoBalanceWorkload(projectId, balancerId);
            res.json({
                success: true,
                data: result
            });
        }
        catch (error) {
            res.status(error.message.includes('Permission') ? 403 : 400).json({
                success: false,
                error: error.message
            });
        }
    }
}
exports.AssignmentController = AssignmentController;
//# sourceMappingURL=assignment.controller.js.map