import { listFindings, getFinding, updateFindingStatus, deleteFinding, quarantineFile, getStats, } from '../../xray/findings-store.js';
import { getGuidance, isLikelySystemFile } from '../../xray/guidance.js';
/** Enrich a finding with guidance and system-file flag */
function enrichFinding(f) {
    const guidance = getGuidance(f.category, f.severity);
    const systemFile = isLikelySystemFile(f.file || f.target);
    return { ...f, guidance, systemFile };
}
export function registerXRayFindingRoutes(app, requireNotLocked) {
    // List findings with optional filters
    app.get('/api/xray/findings', (req, res) => {
        const status = req.query.status;
        const target = req.query.target;
        const severity = req.query.severity;
        const limit = Number(req.query.limit) || 100;
        const findings = listFindings({ status, target, severity, limit }).map(enrichFinding);
        res.json({ findings });
    });
    // Get finding stats
    app.get('/api/xray/findings/stats', (_req, res) => {
        res.json(getStats());
    });
    // Get single finding
    app.get('/api/xray/findings/:id', (req, res) => {
        const finding = getFinding(String(req.params.id));
        if (!finding)
            return res.status(404).json({ error: 'Finding not found' });
        res.json({ finding: enrichFinding(finding) });
    });
    // Update finding status (acknowledge, ignore, resolve)
    app.patch('/api/xray/findings/:id', requireNotLocked, (req, res) => {
        const { status, note } = req.body;
        if (!status || !['reviewed', 'ignored', 'resolved'].includes(status)) {
            return res.status(400).json({ error: 'status must be one of: reviewed, ignored, resolved' });
        }
        const updated = updateFindingStatus(String(req.params.id), status, note);
        if (!updated)
            return res.status(404).json({ error: 'Finding not found' });
        res.json({ finding: updated });
    });
    // Quarantine the file associated with a finding
    app.post('/api/xray/findings/:id/quarantine', requireNotLocked, (req, res) => {
        const { note } = req.body;
        const result = quarantineFile(String(req.params.id), note);
        if (result.error && !result.moved) {
            return res.status(result.error === 'Finding not found' ? 404 : 400).json(result);
        }
        res.json(result);
    });
    // Delete a finding record
    app.delete('/api/xray/findings/:id', requireNotLocked, (req, res) => {
        const deleted = deleteFinding(String(req.params.id));
        if (!deleted)
            return res.status(404).json({ error: 'Finding not found' });
        res.json({ deleted: true });
    });
}
