import { predictViolations, getMostLikelyViolation } from '@4runr/sentinel';
import { requireAuth } from '../middleware/auth.js';
import { readRateLimit, writeRateLimit } from '../middleware/rateLimit.js';
export async function sentinelPredictiveRoutes(fastify) {
    fastify.post('/api/sentinel/predict', {
        preHandler: [requireAuth, writeRateLimit],
    }, async (request, reply) => {
        try {
            const { runId, runState: providedRunState } = (request.body || {});
            let runState;
            const { Sentinel } = await import('@4runr/sentinel');
            const sentinel = Sentinel.getInstance();
            const config = sentinel.getConfig();
            if (providedRunState) {
                runState = providedRunState;
            }
            else if (runId) {
                runState = sentinel.getRunStatus(runId);
                if (!runState) {
                    return reply.status(404).send({
                        error: 'Run not found or not being watched'
                    });
                }
            }
            else {
                return reply.status(400).send({
                    error: 'Either runId or runState is required'
                });
            }
            const predictions = predictViolations(runState, config);
            const mostLikely = getMostLikelyViolation(runState, config);
            return {
                success: true,
                runId: runState.runId || runId,
                predictions,
                mostLikelyViolation: mostLikely,
                willViolate: predictions.some(p => p.willViolate),
                summary: {
                    totalPredictions: predictions.length,
                    violationsPredicted: predictions.filter(p => p.willViolate).length,
                    soonestViolation: mostLikely ? {
                        policy: mostLikely.policy,
                        estimatedTime: mostLikely.estimatedTime,
                        confidence: mostLikely.confidence
                    } : null
                }
            };
        }
        catch (error) {
            return reply.status(500).send({
                error: 'Failed to predict violations',
                details: error.message
            });
        }
    });
    fastify.get('/api/sentinel/predict/:runId', {
        preHandler: [requireAuth, readRateLimit],
    }, async (request, reply) => {
        try {
            const { runId } = request.params;
            const { Sentinel } = await import('@4runr/sentinel');
            const sentinel = Sentinel.getInstance();
            const config = sentinel.getConfig();
            const runState = sentinel.getRunStatus(runId);
            if (!runState) {
                return reply.status(404).send({
                    error: 'Run not found or not being watched'
                });
            }
            const predictions = predictViolations(runState, config);
            const mostLikely = getMostLikelyViolation(runState, config);
            return {
                success: true,
                runId,
                predictions,
                mostLikelyViolation: mostLikely,
                willViolate: predictions.some(p => p.willViolate)
            };
        }
        catch (error) {
            return reply.status(500).send({
                error: 'Failed to get predictions',
                details: error.message
            });
        }
    });
}
//# sourceMappingURL=sentinel-predictive.js.map