import { Router } from 'express';
import { z } from 'zod';
import { TaskController } from '../controllers/task.controller';
import { validate, validateMultiple } from '../middleware/validation.middleware';
import { 
  generalLimiter, 
  strictLimiter, 
  bulkLimiter, 
  templateLimiter 
} from '../middleware/rateLimit.middleware';
import {
  CreateTaskSchema,
  UpdateTaskSchema,
  TaskQuerySchema,
  BulkCreateTasksSchema,
  BulkUpdateTasksSchema,
  BulkDeleteTasksSchema,
  DuplicateTaskSchema,
  CreateTemplateFromTaskSchema,
  ApplyTemplateSchema
} from '../validators/task.validator';

const router = Router();
const taskController = new TaskController();

// UUID validation schema for params
const UuidParamsSchema = z.object({
  id: z.string().uuid()
});

/**
 * @route   GET /api/tasks
 * @desc    Get all tasks with pagination and filtering
 * @access  Public
 */
router.get(
  '/',
  generalLimiter,
  validate(TaskQuerySchema, 'query'),
  (req: any, res) => taskController.getTasks(req, res)
);

/**
 * @route   GET /api/tasks/:id
 * @desc    Get a single task by ID
 * @access  Public
 */
router.get(
  '/:id',
  generalLimiter,
  validate(UuidParamsSchema, 'params'),
  (req: any, res) => taskController.getTaskById(req, res)
);

/**
 * @route   POST /api/tasks
 * @desc    Create a new task
 * @access  Public
 */
router.post(
  '/',
  strictLimiter,
  validate(CreateTaskSchema, 'body'),
  (req: any, res) => taskController.createTask(req, res)
);

/**
 * @route   PUT /api/tasks/:id
 * @desc    Update a task
 * @access  Public
 */
router.put(
  '/:id',
  strictLimiter,
  validateMultiple({
    params: UuidParamsSchema,
    body: UpdateTaskSchema
  }),
  (req: any, res) => taskController.updateTask(req, res)
);

/**
 * @route   PATCH /api/tasks/:id
 * @desc    Partially update a task
 * @access  Public
 */
router.patch(
  '/:id',
  strictLimiter,
  validateMultiple({
    params: UuidParamsSchema,
    body: UpdateTaskSchema
  }),
  (req: any, res) => taskController.updateTask(req, res)
);

/**
 * @route   DELETE /api/tasks/:id
 * @desc    Delete a task
 * @access  Public
 */
router.delete(
  '/:id',
  strictLimiter,
  validate(UuidParamsSchema, 'params'),
  (req: any, res) => taskController.deleteTask(req, res)
);

/**
 * @route   POST /api/tasks/bulk/create
 * @desc    Bulk create tasks
 * @access  Public
 */
router.post(
  '/bulk/create',
  bulkLimiter,
  validate(BulkCreateTasksSchema, 'body'),
  (req: any, res) => taskController.bulkCreateTasks(req, res)
);

/**
 * @route   PUT /api/tasks/bulk/update
 * @desc    Bulk update tasks
 * @access  Public
 */
router.put(
  '/bulk/update',
  bulkLimiter,
  validate(BulkUpdateTasksSchema, 'body'),
  (req: any, res) => taskController.bulkUpdateTasks(req, res)
);

/**
 * @route   DELETE /api/tasks/bulk/delete
 * @desc    Bulk delete tasks
 * @access  Public
 */
router.delete(
  '/bulk/delete',
  bulkLimiter,
  validate(BulkDeleteTasksSchema, 'body'),
  (req: any, res) => taskController.bulkDeleteTasks(req, res)
);

/**
 * @route   POST /api/tasks/duplicate
 * @desc    Duplicate a task
 * @access  Public
 */
router.post(
  '/duplicate',
  strictLimiter,
  validate(DuplicateTaskSchema, 'body'),
  (req: any, res) => taskController.duplicateTask(req, res)
);

/**
 * @route   POST /api/tasks/template/create
 * @desc    Create a template from a task
 * @access  Public
 */
router.post(
  '/template/create',
  templateLimiter,
  validate(CreateTemplateFromTaskSchema, 'body'),
  (req: any, res) => taskController.createTemplateFromTask(req, res)
);

/**
 * @route   POST /api/tasks/template/apply
 * @desc    Apply a template to create a new task
 * @access  Public
 */
router.post(
  '/template/apply',
  templateLimiter,
  validate(ApplyTemplateSchema, 'body'),
  (req: any, res) => taskController.applyTemplate(req, res)
);

export default router;