import { evaluateQualityGate } from '../../../../modules/tracking/entities/qualityGate/qualityGate.js';
export class RecordReviewCompletionUseCase {
    trackingGateway;
    constructor(trackingGateway) {
        this.trackingGateway = trackingGateway;
    }
    execute(input) {
        const { projectPath, mrId, reviewData } = input;
        const mr = this.trackingGateway.getById(projectPath, mrId);
        if (!mr)
            return null;
        const now = new Date().toISOString();
        const suggestions = reviewData.suggestions ?? 0;
        const threadsOpened = reviewData.threadsOpened ?? 0;
        const threadsClosed = reviewData.threadsClosed ?? 0;
        const event = {
            type: reviewData.type,
            timestamp: now,
            durationMs: reviewData.durationMs,
            score: reviewData.score,
            blocking: reviewData.blocking,
            warnings: reviewData.warnings,
            suggestions,
            threadsOpened,
            threadsClosed,
            diffStats: reviewData.diffStats ?? null,
        };
        this.trackingGateway.recordReviewEvent(projectPath, mrId, event);
        const afterEvent = this.trackingGateway.getById(projectPath, mrId);
        if (!afterEvent)
            return null;
        const openThreads = Math.max(0, afterEvent.openThreads + threadsOpened - threadsClosed);
        const totalThreads = afterEvent.totalThreads + threadsOpened;
        let latestScore = afterEvent.latestScore;
        if (reviewData.score !== null) {
            latestScore = reviewData.score;
        }
        const hasBlockingIssues = reviewData.blocking > 0 || openThreads > 0;
        const threshold = input.qualityThreshold ?? null;
        const gateResult = evaluateQualityGate({
            latestScore,
            blockingIssues: reviewData.blocking + openThreads,
            threshold,
        });
        const nextState = hasBlockingIssues || !gateResult.allowed ? 'pending-fix' : 'pending-approval';
        this.trackingGateway.update(projectPath, mrId, {
            openThreads,
            totalThreads,
            latestScore,
            state: nextState,
            bypass: null,
        });
        return this.trackingGateway.getById(projectPath, mrId);
    }
}
//# sourceMappingURL=recordReviewCompletion.usecase.js.map