"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const zod_1 = require("zod");
const assignment_controller_1 = require("../controllers/assignment.controller");
const validation_middleware_1 = require("../middleware/validation.middleware");
const rbac_middleware_1 = require("../middleware/rbac.middleware");
const rateLimit_middleware_1 = require("../middleware/rateLimit.middleware");
const rbac_service_1 = require("../services/rbac.service");
const router = (0, express_1.Router)();
const assignmentController = new assignment_controller_1.AssignmentController();
// Validation schemas
const AssignTaskSchema = zod_1.z.object({
    taskId: zod_1.z.string().uuid(),
    assigneeId: zod_1.z.string().uuid(),
    dueDate: zod_1.z.string().datetime().optional(),
    workloadPoints: zod_1.z.number().int().min(1).max(100).optional(),
    notes: zod_1.z.string().max(500).optional()
});
const ReassignTaskSchema = zod_1.z.object({
    taskId: zod_1.z.string().uuid(),
    newAssigneeId: zod_1.z.string().uuid(),
    reason: zod_1.z.string().max(500).optional()
});
const BatchAssignSchema = zod_1.z.object({
    assignments: zod_1.z.array(zod_1.z.object({
        taskId: zod_1.z.string().uuid(),
        assigneeId: zod_1.z.string().uuid(),
        dueDate: zod_1.z.string().datetime().optional(),
        workloadPoints: zod_1.z.number().int().min(1).max(100).optional()
    })).min(1).max(50)
});
const GetAssignedTasksSchema = zod_1.z.object({
    includeCompleted: zod_1.z.coerce.boolean().optional(),
    projectId: zod_1.z.string().uuid().optional(),
    status: zod_1.z.string().optional(),
    sort: zod_1.z.string().optional(),
    limit: zod_1.z.coerce.number().int().min(1).max(1000).optional()
});
const GetRecommendationsSchema = zod_1.z.object({
    maxRecommendations: zod_1.z.coerce.number().int().min(1).max(20).optional(),
    projectTeamOnly: zod_1.z.coerce.boolean().optional()
});
/**
 * @route   POST /api/assignments/assign
 * @desc    Assign a task to a user
 * @access  Protected - Requires ASSIGN_TASK permission
 */
router.post('/assign', rbac_middleware_1.authenticateToken, (0, rbac_middleware_1.requirePermission)(rbac_service_1.Permission.ASSIGN_TASK), rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(AssignTaskSchema, 'body'), (req, res) => assignmentController.assignTask(req, res));
/**
 * @route   POST /api/assignments/reassign
 * @desc    Reassign a task to a different user
 * @access  Protected - Requires ASSIGN_TASK permission
 */
router.post('/reassign', rbac_middleware_1.authenticateToken, (0, rbac_middleware_1.requirePermission)(rbac_service_1.Permission.ASSIGN_TASK), rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(ReassignTaskSchema, 'body'), (req, res) => assignmentController.reassignTask(req, res));
/**
 * @route   DELETE /api/assignments/:taskId
 * @desc    Unassign a task
 * @access  Protected - Requires ASSIGN_TASK permission
 */
router.delete('/:taskId', rbac_middleware_1.authenticateToken, (0, rbac_middleware_1.requirePermission)(rbac_service_1.Permission.ASSIGN_TASK), rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({ taskId: zod_1.z.string().uuid() }), 'params'), (req, res) => assignmentController.unassignTask(req, res));
/**
 * @route   GET /api/assignments/user/:userId
 * @desc    Get tasks assigned to a user
 * @access  Protected - User can view their own or requires VIEW_USERS permission
 */
router.get('/user/:userId', rbac_middleware_1.authenticateToken, rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validateMultiple)({
    params: zod_1.z.object({ userId: zod_1.z.string().uuid() }),
    query: GetAssignedTasksSchema
}), (req, res) => assignmentController.getAssignedTasks(req, res));
/**
 * @route   GET /api/assignments/workload/:userId
 * @desc    Get workload summary for a user
 * @access  Protected - User can view their own or requires VIEW_USERS permission
 */
router.get('/workload/:userId', rbac_middleware_1.authenticateToken, rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({ userId: zod_1.z.string().uuid() }), 'params'), (req, res) => assignmentController.getWorkloadSummary(req, res));
/**
 * @route   GET /api/assignments/recommendations/:taskId
 * @desc    Get assignment recommendations for a task
 * @access  Protected - Requires ASSIGN_TASK permission
 */
router.get('/recommendations/:taskId', rbac_middleware_1.authenticateToken, (0, rbac_middleware_1.requirePermission)(rbac_service_1.Permission.ASSIGN_TASK), rateLimit_middleware_1.generalLimiter, (0, validation_middleware_1.validateMultiple)({
    params: zod_1.z.object({ taskId: zod_1.z.string().uuid() }),
    query: GetRecommendationsSchema
}), (req, res) => assignmentController.getAssignmentRecommendations(req, res));
/**
 * @route   POST /api/assignments/batch
 * @desc    Batch assign multiple tasks
 * @access  Protected - Requires ASSIGN_TASK permission
 */
router.post('/batch', rbac_middleware_1.authenticateToken, (0, rbac_middleware_1.requirePermission)(rbac_service_1.Permission.ASSIGN_TASK), rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(BatchAssignSchema, 'body'), (req, res) => assignmentController.batchAssignTasks(req, res));
/**
 * @route   POST /api/assignments/auto-balance/:projectId
 * @desc    Auto-balance workload across project team
 * @access  Protected - Requires ASSIGN_TASK and MANAGE_PROJECT_MEMBERS permissions
 */
router.post('/auto-balance/:projectId', rbac_middleware_1.authenticateToken, (0, rbac_middleware_1.requirePermission)(rbac_service_1.Permission.ASSIGN_TASK), rateLimit_middleware_1.strictLimiter, (0, validation_middleware_1.validate)(zod_1.z.object({ projectId: zod_1.z.string().uuid() }), 'params'), (req, res) => assignmentController.autoBalanceWorkload(req, res));
exports.default = router;
//# sourceMappingURL=assignment.routes.js.map