"use strict";
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 serviceManager_1 = require("../serviceManager");
const checkAuth_1 = require("../middleware/checkAuth");
const fs_extra_1 = __importDefault(require("fs-extra"));
const router = (0, express_1.Router)();
router.get('/', (req, res) => {
    try {
        const services = serviceManager_1.serviceManager.list();
        res.json(services);
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'An unknown error occurred';
        res.status(500).json({ error: message });
    }
});
router.post('/start', checkAuth_1.checkAuth, async (req, res) => {
    const { path, name } = req.body;
    if (!path) {
        return res.status(400).json({ error: 'Project path is required.' });
    }
    try {
        const service = await serviceManager_1.serviceManager.start(path, name);
        res.status(201).json(service);
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'An unknown error occurred';
        res.status(500).json({ error: message });
    }
});
router.post('/:name/stop', async (req, res) => {
    try {
        const service = await serviceManager_1.serviceManager.stop(req.params.name);
        res.json(service);
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'An unknown error occurred';
        res.status(404).json({ error: message });
    }
});
router.post('/:name/restart', async (req, res) => {
    try {
        const service = await serviceManager_1.serviceManager.restart(req.params.name);
        res.json(service);
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'An unknown error occurred';
        res.status(404).json({ error: message });
    }
});
router.delete('/:name', async (req, res) => {
    try {
        const result = await serviceManager_1.serviceManager.remove(req.params.name);
        res.status(200).json(result);
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'An unknown error occurred';
        res.status(404).json({ error: message });
    }
});
router.post('/remove-all', async (req, res) => {
    try {
        const result = await serviceManager_1.serviceManager.removeAll();
        res.json(result);
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'An unknown error occurred';
        res.status(500).json({ error: message });
    }
});
router.get('/:name/logs', async (req, res) => {
    try {
        const logPaths = serviceManager_1.serviceManager.getLogPaths(req.params.name);
        const readLogFile = async (filePath) => {
            try {
                return await fs_extra_1.default.readFile(filePath, 'utf-8');
            }
            catch (error) {
                if (error.code === 'ENOENT') {
                    return '(not found)';
                }
                console.error(`Error reading log file ${filePath}:`, error);
                return `(error reading file: ${error.message})`;
            }
        };
        const out = await readLogFile(logPaths.out);
        const err = await readLogFile(logPaths.err);
        res.json({ out, err });
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'An unknown error occurred';
        res.status(404).json({ error: message });
    }
});
exports.default = router;
