"use strict";
/**
 * Cron Jobs API Routes
 * Manages scheduled tasks using PM2's cron_restart feature
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const CronJobService_1 = __importDefault(require("../services/CronJobService"));
const router = (0, express_1.Router)();
/**
 * GET /api/cron-jobs
 * Get all cron jobs
 */
router.get('/', async (req, res) => {
    try {
        const jobs = CronJobService_1.default.getCronJobs();
        res.json({ success: true, data: jobs });
    }
    catch (error) {
        console.error('Error getting cron jobs:', error);
        res.status(500).json({ success: false, error: error.message });
    }
});
/**
 * GET /api/cron-jobs/status
 * Get status of all cron jobs including PM2 process info
 */
router.get('/status', async (req, res) => {
    try {
        const statuses = await CronJobService_1.default.getCronJobsStatus();
        res.json({ success: true, data: statuses });
    }
    catch (error) {
        console.error('Error getting cron job statuses:', error);
        res.status(500).json({ success: false, error: error.message });
    }
});
/**
 * GET /api/cron-jobs/:id
 * Get a specific cron job by ID
 */
router.get('/:id', (req, res) => {
    try {
        const job = CronJobService_1.default.getCronJob(req.params.id);
        if (!job) {
            return res.status(404).json({ success: false, error: 'Cron job not found' });
        }
        res.json({ success: true, data: job });
    }
    catch (error) {
        console.error('Error getting cron job:', error);
        res.status(500).json({ success: false, error: error.message });
    }
});
/**
 * POST /api/cron-jobs
 * Create a new cron job
 */
router.post('/', async (req, res) => {
    try {
        const config = req.body;
        // Validate required fields
        if (!config.name || !config.scriptType || !config.cronExpression) {
            return res.status(400).json({
                success: false,
                error: 'Missing required fields: name, scriptType, cronExpression'
            });
        }
        // Validate script mode specific requirements
        if (config.scriptMode === 'file' && !config.scriptPath) {
            return res.status(400).json({
                success: false,
                error: 'Script path is required when using file mode'
            });
        }
        if (config.scriptMode === 'inline' && !config.inlineScript) {
            return res.status(400).json({
                success: false,
                error: 'Inline script content is required when using inline mode'
            });
        }
        const job = await CronJobService_1.default.createCronJob(config);
        res.json({ success: true, data: job });
    }
    catch (error) {
        console.error('Error creating cron job:', error);
        res.status(400).json({ success: false, error: error.message });
    }
});
/**
 * PUT /api/cron-jobs/:id
 * Update a cron job
 */
router.put('/:id', async (req, res) => {
    try {
        const updates = req.body;
        const job = await CronJobService_1.default.updateCronJob(req.params.id, updates);
        res.json({ success: true, data: job });
    }
    catch (error) {
        console.error('Error updating cron job:', error);
        res.status(400).json({ success: false, error: error.message });
    }
});
/**
 * DELETE /api/cron-jobs/:id
 * Delete a cron job
 */
router.delete('/:id', async (req, res) => {
    try {
        await CronJobService_1.default.deleteCronJob(req.params.id);
        res.json({ success: true, message: 'Cron job deleted successfully' });
    }
    catch (error) {
        console.error('Error deleting cron job:', error);
        res.status(400).json({ success: false, error: error.message });
    }
});
/**
 * POST /api/cron-jobs/:id/toggle
 * Toggle cron job enabled state
 */
router.post('/:id/toggle', async (req, res) => {
    try {
        const job = await CronJobService_1.default.toggleCronJob(req.params.id);
        res.json({ success: true, data: job });
    }
    catch (error) {
        console.error('Error toggling cron job:', error);
        res.status(400).json({ success: false, error: error.message });
    }
});
/**
 * POST /api/cron-jobs/:id/start
 * Manually start a cron job
 */
router.post('/:id/start', async (req, res) => {
    try {
        await CronJobService_1.default.startCronJob(req.params.id);
        res.json({ success: true, message: 'Cron job started successfully' });
    }
    catch (error) {
        console.error('Error starting cron job:', error);
        res.status(400).json({ success: false, error: error.message });
    }
});
/**
 * POST /api/cron-jobs/:id/stop
 * Manually stop a cron job
 */
router.post('/:id/stop', async (req, res) => {
    try {
        await CronJobService_1.default.stopCronJob(req.params.id);
        res.json({ success: true, message: 'Cron job stopped successfully' });
    }
    catch (error) {
        console.error('Error stopping cron job:', error);
        res.status(400).json({ success: false, error: error.message });
    }
});
/**
 * POST /api/cron-jobs/validate
 * Validate a cron expression
 */
router.post('/validate', (req, res) => {
    try {
        const { expression } = req.body;
        if (!expression) {
            return res.status(400).json({ success: false, error: 'Expression is required' });
        }
        const validation = CronJobService_1.default.validateCronExpression(expression);
        const description = CronJobService_1.default.getCronDescription(expression);
        res.json({
            success: true,
            data: {
                ...validation,
                description
            }
        });
    }
    catch (error) {
        console.error('Error validating cron expression:', error);
        res.status(400).json({ success: false, error: error.message });
    }
});
exports.default = router;
