"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 auth_1 = require("../middleware/auth");
const enterpriseDb_1 = require("../services/enterpriseDb");
const analysisResultSnapshotStore_1 = require("../services/analysisResultSnapshotStore");
const rbac_1 = require("../services/rbac");
const VALID_SCENE_TYPES = new Set([
    'startup',
    'scrolling',
    'interaction',
    'memory',
    'cpu',
    'general',
]);
const VALID_VISIBILITIES = new Set(['private', 'workspace']);
function optionalString(value) {
    return typeof value === 'string' && value.trim().length > 0 ? value.trim() : undefined;
}
function parseLimit(value) {
    if (value === undefined)
        return undefined;
    const parsed = Number(value);
    if (!Number.isInteger(parsed) || parsed < 1 || parsed > 500) {
        throw new Error('limit must be an integer between 1 and 500');
    }
    return parsed;
}
const router = express_1.default.Router();
router.get('/', (req, res) => {
    const context = (0, auth_1.requireRequestContext)(req);
    if (!(0, rbac_1.hasRbacPermission)(context, 'analysis_result:read')) {
        (0, rbac_1.sendForbidden)(res, 'analysis_result:read permission is required');
        return;
    }
    const sceneType = optionalString(req.query.sceneType);
    if (sceneType && !VALID_SCENE_TYPES.has(sceneType)) {
        res.status(400).json({
            success: false,
            error: 'Invalid sceneType',
        });
        return;
    }
    const visibility = optionalString(req.query.visibility);
    if (visibility && !VALID_VISIBILITIES.has(visibility)) {
        res.status(400).json({
            success: false,
            error: 'Invalid visibility',
        });
        return;
    }
    let limit;
    try {
        limit = parseLimit(req.query.limit);
    }
    catch (error) {
        res.status(400).json({
            success: false,
            error: error instanceof Error ? error.message : 'Invalid limit',
        });
        return;
    }
    const db = (0, enterpriseDb_1.openEnterpriseDb)();
    try {
        const repository = (0, analysisResultSnapshotStore_1.createAnalysisResultSnapshotRepository)(db);
        const results = repository.listSnapshots({
            tenantId: context.tenantId,
            workspaceId: context.workspaceId,
            userId: context.userId,
        }, {
            traceId: optionalString(req.query.traceId),
            sceneType: sceneType,
            visibility: visibility,
            createdBy: optionalString(req.query.createdBy),
            limit,
        });
        res.json({
            success: true,
            results,
            count: results.length,
        });
    }
    catch (error) {
        console.error('[AnalysisResultRoutes] Failed to list analysis results:', error);
        res.status(500).json({
            success: false,
            error: 'Failed to list analysis results',
        });
    }
    finally {
        db.close();
    }
});
router.get('/:snapshotId', (req, res) => {
    const context = (0, auth_1.requireRequestContext)(req);
    if (!(0, rbac_1.hasRbacPermission)(context, 'analysis_result:read')) {
        (0, rbac_1.sendForbidden)(res, 'analysis_result:read permission is required');
        return;
    }
    const snapshotId = optionalString(req.params.snapshotId);
    if (!snapshotId) {
        res.status(400).json({
            success: false,
            error: 'snapshotId is required',
        });
        return;
    }
    const db = (0, enterpriseDb_1.openEnterpriseDb)();
    try {
        const repository = (0, analysisResultSnapshotStore_1.createAnalysisResultSnapshotRepository)(db);
        const snapshot = repository.getSnapshot({
            tenantId: context.tenantId,
            workspaceId: context.workspaceId,
            userId: context.userId,
        }, snapshotId);
        if (!snapshot) {
            res.status(404).json({
                success: false,
                error: 'Analysis result snapshot not found',
            });
            return;
        }
        res.json({
            success: true,
            snapshot,
        });
    }
    catch (error) {
        console.error('[AnalysisResultRoutes] Failed to read analysis result:', error);
        res.status(500).json({
            success: false,
            error: 'Failed to read analysis result',
        });
    }
    finally {
        db.close();
    }
});
router.patch('/:snapshotId', (req, res) => {
    const context = (0, auth_1.requireRequestContext)(req);
    const snapshotId = optionalString(req.params.snapshotId);
    if (!snapshotId) {
        res.status(400).json({
            success: false,
            error: 'snapshotId is required',
        });
        return;
    }
    const visibility = optionalString(req.body?.visibility);
    if (!visibility || !VALID_VISIBILITIES.has(visibility)) {
        res.status(400).json({
            success: false,
            error: 'Invalid visibility',
        });
        return;
    }
    const db = (0, enterpriseDb_1.openEnterpriseDb)();
    try {
        const repository = (0, analysisResultSnapshotStore_1.createAnalysisResultSnapshotRepository)(db);
        const existing = repository.getSnapshot({
            tenantId: context.tenantId,
            workspaceId: context.workspaceId,
            userId: context.userId,
        }, snapshotId);
        if (!existing) {
            res.status(404).json({
                success: false,
                error: 'Analysis result snapshot not found',
            });
            return;
        }
        if (!(0, rbac_1.canShareAnalysisResultResource)({
            tenantId: existing.tenantId,
            workspaceId: existing.workspaceId,
            userId: existing.createdBy,
        }, context)) {
            (0, rbac_1.sendForbidden)(res, 'analysis_result:share permission is required');
            return;
        }
        const updated = repository.updateVisibility({
            tenantId: context.tenantId,
            workspaceId: context.workspaceId,
            userId: context.userId,
            auditActorUserId: context.userId,
        }, snapshotId, visibility);
        if (!updated) {
            res.status(404).json({
                success: false,
                error: 'Analysis result snapshot not found',
            });
            return;
        }
        res.json({
            success: true,
            snapshot: updated,
        });
    }
    catch (error) {
        console.error('[AnalysisResultRoutes] Failed to update analysis result:', error);
        res.status(500).json({
            success: false,
            error: 'Failed to update analysis result',
        });
    }
    finally {
        db.close();
    }
});
exports.default = router;
//# sourceMappingURL=analysisResultRoutes.js.map