import { GithubWebhookMensagens } from '../../../core/messages/index.js';
import {} from './octokit.js';
export const MAX_ANNOTATIONS = 50;
export const PASS_THRESHOLD = 70;
export const DEFAULT_SCORE = 100;
export const CHECK_RUN_NAME = 'Tutanus GitHub Actions Analyzer';
function nivelParaAnnotation(nivel) {
    if (nivel === 'erro')
        return 'failure';
    if (nivel === 'aviso')
        return 'warning';
    return 'notice';
}
export function gerarResumoAnalise(scoreGeral, ocorrencias) {
    let summary = `## Análise de Workflows do GitHub Actions\n\n**Score Geral:** ${scoreGeral}/100\n\n`;
    if (ocorrencias.length > 0) {
        summary += `### Ocorrências (${ocorrencias.length})\n\n`;
        for (const ocorrencia of ocorrencias.slice(0, MAX_ANNOTATIONS)) {
            summary += `- **${ocorrencia.nivel?.toUpperCase() || 'INFO'}**: \`${ocorrencia.relPath}:${ocorrencia.linha || 1}\` - ${ocorrencia.mensagem}\n`;
        }
        if (ocorrencias.length > MAX_ANNOTATIONS) {
            summary += `\n*... e mais ${ocorrencias.length - MAX_ANNOTATIONS} ocorrências.*`;
        }
    }
    else {
        summary += `Nenhuma ocorrência encontrada. Excelente!\n`;
    }
    return summary;
}
export function gerarAnnotations(ocorrencias) {
    return ocorrencias.slice(0, MAX_ANNOTATIONS).map(ocorrencia => {
        const linha = ocorrencia.linha || 1;
        return {
            path: ocorrencia.relPath,
            start_line: linha,
            end_line: linha,
            annotation_level: nivelParaAnnotation(ocorrencia.nivel),
            message: ocorrencia.mensagem
        };
    });
}
export async function completarCheckRun(octokit, owner, repo, checkRunId, scoreGeral, ocorrencias, options = {}) {
    const { status = 'success' } = options;
    const conclusion = scoreGeral >= PASS_THRESHOLD ? 'success' : 'failure';
    const finalConclusion = status === 'failure' ? 'failure' : conclusion;
    try {
        await octokit.request('PATCH /repos/{owner}/{repo}/check-runs/{check_run_id}', {
            owner,
            repo,
            check_run_id: checkRunId,
            status: 'completed',
            conclusion: finalConclusion,
            output: {
                title: GithubWebhookMensagens.checkRunTitle(scoreGeral),
                summary: gerarResumoAnalise(scoreGeral, ocorrencias),
                annotations: gerarAnnotations(ocorrencias)
            }
        });
    }
    catch (err) {
        const error = err;
        console.error(GithubWebhookMensagens.erroAnotacaoCheckRun(error.message), err);
        throw err;
    }
}
//# sourceMappingURL=check-run-builder.js.map