import { OverviewPresenter, } from '../../../../../modules/statistics-insights/interface-adapters/presenters/overview.presenter.js';
function buildSummary(stats) {
    return {
        totalReviews: stats.totalReviews,
        averageScore: stats.averageScore,
        averageDuration: stats.averageDuration,
        totalBlocking: stats.totalBlocking,
        totalWarnings: stats.totalWarnings,
    };
}
function collectProjectConfigs(gateway, repositories) {
    if (!gateway)
        return undefined;
    const summaries = {};
    for (const repository of repositories) {
        const result = gateway.read(repository.localPath);
        if (result.status === 'ok' && typeof result.config.externalLink === 'string' && result.config.externalLink.length > 0) {
            summaries[repository.localPath] = { externalLink: result.config.externalLink };
        }
    }
    return summaries;
}
export const overviewRoutes = async (fastify, options) => {
    const presenter = new OverviewPresenter();
    fastify.get('/api/overview', async () => {
        const repositories = options.getRepositories();
        const enabledRepositories = repositories.filter((repository) => repository.enabled);
        const projectStats = [];
        const recentReviews = [];
        for (const repository of enabledRepositories) {
            const stats = options.statsGateway.loadProjectStats(repository.localPath);
            if (stats) {
                projectStats.push({
                    project: repository.name,
                    path: repository.localPath,
                    stats,
                    summary: buildSummary(stats),
                });
            }
            const reviews = await options.reviewFileGateway.listReviews(repository.localPath);
            recentReviews.push(...reviews);
        }
        const projectConfigs = collectProjectConfigs(options.projectConfigGateway, repositories);
        const capacity = options.getCapacity ? options.getCapacity() : { running: 0, max: 0 };
        return presenter.present({
            repositories,
            activeJobs: options.getActiveJobs(),
            projectStats,
            recentReviews,
            projectConfigs,
            capacity,
        });
    });
};
//# sourceMappingURL=overview.routes.js.map