"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskController = void 0;
const task_service_1 = require("../services/task.service");
class TaskController {
    constructor() {
        this.taskService = new task_service_1.TaskService();
    }
    /**
     * Create a new task
     */
    async createTask(req, res) {
        try {
            const task = await this.taskService.createTask(req.body);
            res.status(201).json({
                success: true,
                data: task,
                message: 'Task created successfully'
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message || 'Failed to create task'
            });
        }
    }
    /**
     * Get all tasks with pagination
     */
    async getTasks(req, res) {
        try {
            const result = await this.taskService.getTasks(req.query);
            res.json({
                success: true,
                ...result
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message || 'Failed to fetch tasks'
            });
        }
    }
    /**
     * Get a single task by ID
     */
    async getTaskById(req, res) {
        try {
            const task = await this.taskService.getTaskById(req.params.id);
            res.json({
                success: true,
                data: task
            });
        }
        catch (error) {
            const statusCode = error.message === 'Task not found' ? 404 : 500;
            res.status(statusCode).json({
                success: false,
                error: error.message || 'Failed to fetch task'
            });
        }
    }
    /**
     * Update a task
     */
    async updateTask(req, res) {
        try {
            const task = await this.taskService.updateTask(req.params.id, req.body);
            res.json({
                success: true,
                data: task,
                message: 'Task updated successfully'
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message || 'Failed to update task'
            });
        }
    }
    /**
     * Delete a task
     */
    async deleteTask(req, res) {
        try {
            await this.taskService.deleteTask(req.params.id);
            res.json({
                success: true,
                message: 'Task deleted successfully'
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message || 'Failed to delete task'
            });
        }
    }
    /**
     * Bulk create tasks
     */
    async bulkCreateTasks(req, res) {
        try {
            const tasks = await this.taskService.bulkCreateTasks(req.body);
            res.status(201).json({
                success: true,
                data: tasks,
                message: `${tasks.length} tasks created successfully`
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message || 'Failed to bulk create tasks'
            });
        }
    }
    /**
     * Bulk update tasks
     */
    async bulkUpdateTasks(req, res) {
        try {
            const tasks = await this.taskService.bulkUpdateTasks(req.body);
            res.json({
                success: true,
                data: tasks,
                message: `${tasks.length} tasks updated successfully`
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message || 'Failed to bulk update tasks'
            });
        }
    }
    /**
     * Bulk delete tasks
     */
    async bulkDeleteTasks(req, res) {
        try {
            await this.taskService.bulkDeleteTasks(req.body);
            res.json({
                success: true,
                message: `${req.body.ids.length} tasks deleted successfully`
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message || 'Failed to bulk delete tasks'
            });
        }
    }
    /**
     * Duplicate a task
     */
    async duplicateTask(req, res) {
        try {
            const task = await this.taskService.duplicateTask(req.body);
            res.status(201).json({
                success: true,
                data: task,
                message: 'Task duplicated successfully'
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message || 'Failed to duplicate task'
            });
        }
    }
    /**
     * Create a template from a task
     */
    async createTemplateFromTask(req, res) {
        try {
            const template = await this.taskService.createTemplateFromTask(req.body);
            res.status(201).json({
                success: true,
                data: template,
                message: 'Template created successfully from task'
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message || 'Failed to create template from task'
            });
        }
    }
    /**
     * Apply a template to create a new task
     */
    async applyTemplate(req, res) {
        try {
            const task = await this.taskService.applyTemplate(req.body);
            res.status(201).json({
                success: true,
                data: task,
                message: 'Task created successfully from template'
            });
        }
        catch (error) {
            res.status(500).json({
                success: false,
                error: error.message || 'Failed to apply template'
            });
        }
    }
}
exports.TaskController = TaskController;
//# sourceMappingURL=task.controller.js.map