"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const flamegraphAiSummary_1 = require("../services/flamegraphAiSummary");
const flamegraphAnalyzer_1 = require("../services/flamegraphAnalyzer");
const traceProcessorService_1 = require("../services/traceProcessorService");
const router = express_1.default.Router();
const traceProcessorService = (0, traceProcessorService_1.getTraceProcessorService)();
function errorMessage(error, fallback) {
    return error instanceof Error ? error.message : fallback;
}
async function ensureTrace(traceId) {
    const trace = await traceProcessorService.getOrLoadTrace(traceId);
    return !!trace;
}
router.get('/:traceId/availability', async (req, res) => {
    try {
        const { traceId } = req.params;
        if (!traceId) {
            return res.status(400).json({ success: false, error: 'traceId is required' });
        }
        if (!(await ensureTrace(traceId))) {
            return res.status(404).json({ success: false, error: `Trace ${traceId} not found` });
        }
        const availability = await (0, flamegraphAnalyzer_1.getFlamegraphAvailability)(traceProcessorService, traceId);
        res.json({ success: true, ...availability });
    }
    catch (error) {
        console.error('[Flamegraph] Availability error:', error);
        res.status(500).json({
            success: false,
            error: errorMessage(error, 'Failed to check flamegraph availability'),
        });
    }
});
router.post('/:traceId/analyze', async (req, res) => {
    try {
        const { traceId } = req.params;
        if (!traceId) {
            return res.status(400).json({ success: false, error: 'traceId is required' });
        }
        if (!(await ensureTrace(traceId))) {
            return res.status(404).json({ success: false, error: `Trace ${traceId} not found` });
        }
        const options = (req.body || {});
        const analysis = await (0, flamegraphAnalyzer_1.analyzeFlamegraph)(traceProcessorService, traceId, options);
        const aiSummary = options.includeAi === false ? undefined : await (0, flamegraphAiSummary_1.summarizeFlamegraphWithAi)(analysis, options.question);
        res.json({
            success: true,
            analysis,
            aiSummary,
        });
    }
    catch (error) {
        console.error('[Flamegraph] Analyze error:', error);
        res.status(500).json({
            success: false,
            error: errorMessage(error, 'Flamegraph analysis failed'),
        });
    }
});
router.post('/:traceId/summarize', async (req, res) => {
    try {
        const { traceId } = req.params;
        if (!traceId) {
            return res.status(400).json({ success: false, error: 'traceId is required' });
        }
        const body = req.body || {};
        const analysis = body.analysis;
        if (!analysis) {
            if (!(await ensureTrace(traceId))) {
                return res.status(404).json({ success: false, error: `Trace ${traceId} not found` });
            }
            const generatedAnalysis = await (0, flamegraphAnalyzer_1.analyzeFlamegraph)(traceProcessorService, traceId, {
                ...(body.options || {}),
                includeAi: false,
            });
            const aiSummary = await (0, flamegraphAiSummary_1.summarizeFlamegraphWithAi)(generatedAnalysis, body.question);
            return res.json({ success: true, analysis: generatedAnalysis, aiSummary });
        }
        const aiSummary = await (0, flamegraphAiSummary_1.summarizeFlamegraphWithAi)(analysis, body.question);
        res.json({ success: true, aiSummary });
    }
    catch (error) {
        console.error('[Flamegraph] Summarize error:', error);
        res.status(500).json({
            success: false,
            error: errorMessage(error, 'Flamegraph summarization failed'),
        });
    }
});
exports.default = router;
//# sourceMappingURL=flamegraphRoutes.js.map