import { computeInsightsWithPersistence } from '../../../../modules/statistics-insights/usecases/insights/computeInsightsWithPersistence.usecase.js';
const EMPTY_TEAM_INSIGHT = {
    developerCount: 0,
    totalReviewCount: 0,
    averageLevels: { quality: 5, responsiveness: 5, codeVolume: 5, iteration: 5 },
    strengths: [],
    weaknesses: [],
    tips: [],
};
function computeHasNewReviewsSinceAiGeneration(currentReviewCount, reviewCountAtAiGeneration, aiGeneratedAt) {
    if (!aiGeneratedAt)
        return false;
    return currentReviewCount > reviewCountAtAiGeneration;
}
export function getInsightsWithAiStatus(input) {
    const { projectPath, statsGateway, insightsGateway } = input;
    const stats = statsGateway.loadProjectStats(projectPath);
    const persistedData = insightsGateway.loadPersistedInsights(projectPath);
    if (!stats || stats.reviews.length === 0) {
        if (persistedData) {
            const result = computeInsightsWithPersistence([], persistedData);
            insightsGateway.savePersistedInsights(projectPath, {
                ...result.persistedData,
                aiInsights: persistedData.aiInsights ?? null,
                reviewCountAtAiGeneration: persistedData.reviewCountAtAiGeneration ?? 0,
            });
            const hasNewReviewsSinceAiGeneration = computeHasNewReviewsSinceAiGeneration(result.persistedData.processedReviewIds.length, persistedData.reviewCountAtAiGeneration ?? 0, persistedData.aiInsights?.generatedAt ?? null);
            return {
                developerInsights: result.developerInsights,
                teamInsight: result.teamInsight,
                aiInsights: persistedData.aiInsights ?? null,
                hasNewReviewsSinceAiGeneration,
            };
        }
        return {
            developerInsights: [],
            teamInsight: EMPTY_TEAM_INSIGHT,
            aiInsights: null,
            hasNewReviewsSinceAiGeneration: false,
        };
    }
    const result = computeInsightsWithPersistence(stats.reviews, persistedData);
    insightsGateway.savePersistedInsights(projectPath, {
        ...result.persistedData,
        aiInsights: persistedData?.aiInsights ?? null,
        reviewCountAtAiGeneration: persistedData?.reviewCountAtAiGeneration ?? 0,
    });
    const hasNewReviewsSinceAiGeneration = computeHasNewReviewsSinceAiGeneration(result.persistedData.processedReviewIds.length, persistedData?.reviewCountAtAiGeneration ?? 0, persistedData?.aiInsights?.generatedAt ?? null);
    return {
        developerInsights: result.developerInsights,
        teamInsight: result.teamInsight,
        aiInsights: persistedData?.aiInsights ?? null,
        hasNewReviewsSinceAiGeneration,
    };
}
//# sourceMappingURL=getInsightsWithAiStatus.usecase.js.map