import { aiInsightsRawResponseSchema } from '../../../../modules/statistics-insights/entities/insight/aiInsight.schema.js';
import { buildAiInsightsPrompt } from '../../../../modules/statistics-insights/usecases/insights/buildAiInsightsPrompt.js';
import { computeInsightsWithPersistence } from '../../../../modules/statistics-insights/usecases/insights/computeInsightsWithPersistence.usecase.js';
function stripMarkdownFences(text) {
    let cleaned = text.trim();
    if (cleaned.startsWith('```')) {
        const firstNewline = cleaned.indexOf('\n');
        if (firstNewline !== -1) {
            cleaned = cleaned.substring(firstNewline + 1);
        }
    }
    if (cleaned.endsWith('```')) {
        cleaned = cleaned.substring(0, cleaned.length - 3).trim();
    }
    return cleaned;
}
function parseClaudeResponse(rawOutput) {
    const cleaned = stripMarkdownFences(rawOutput);
    const parsed = JSON.parse(cleaned);
    return aiInsightsRawResponseSchema.parse(parsed);
}
export async function generateAiInsights(input) {
    const { projectPath, statsGateway, reviewFileGateway, reviewRequestTrackingGateway, logger, claudeInvoker, language, } = input;
    const stats = statsGateway.loadProjectStats(projectPath);
    if (!stats || stats.reviews.length === 0) {
        throw new Error('No review statistics available for this project');
    }
    const reviewFiles = await reviewFileGateway.listReviews(projectPath);
    const reviewContents = new Map();
    for (const reviewFile of reviewFiles) {
        const content = await reviewFileGateway.readReview(projectPath, reviewFile.filename);
        if (content) {
            reviewContents.set(reviewFile.mrNumber, content);
        }
    }
    const trackingData = reviewRequestTrackingGateway.loadTracking(projectPath);
    const trackedMrs = trackingData?.mrs ?? [];
    const prompt = buildAiInsightsPrompt({
        reviews: stats.reviews,
        reviewContents,
        trackedMrs,
        language,
    });
    logger.info({ promptLength: prompt.length }, 'Sending prompt to Claude for AI insights');
    const rawOutput = await claudeInvoker(prompt);
    logger.info({ outputLength: rawOutput.length }, 'Received Claude response for AI insights');
    const result = parseClaudeResponse(rawOutput);
    return {
        ...result,
        generatedAt: new Date().toISOString(),
    };
}
export function persistAiInsightsResult(input) {
    const { projectPath, aiInsights, statsGateway, insightsGateway } = input;
    const existingData = insightsGateway.loadPersistedInsights(projectPath);
    const stats = statsGateway.loadProjectStats(projectPath);
    const currentReviews = stats?.reviews ?? [];
    const upToDateResult = computeInsightsWithPersistence(currentReviews, existingData);
    insightsGateway.savePersistedInsights(projectPath, {
        ...upToDateResult.persistedData,
        aiInsights,
        reviewCountAtAiGeneration: upToDateResult.persistedData.processedReviewIds.length,
    });
}
//# sourceMappingURL=generateAiInsights.usecase.js.map