import { cleanupExpiredReviews } from '../../../../../modules/data-lifecycle/usecases/cleanup/cleanupExpiredReviews.usecase.js';
import { getProjectRetentionDays } from '../../../../../config/projectConfig.js';
export const cleanupRoutes = async (fastify, options) => {
    const { reviewFileGateway, reviewLogFileGateway, getRepositories, logger } = options;
    fastify.post('/api/reviews/cleanup', async (request) => {
        const query = request.query;
        const singleProjectPath = query.path?.trim();
        const allDeletedFiles = [];
        let totalDeletedCount = 0;
        if (singleProjectPath) {
            const retentionDays = getProjectRetentionDays(singleProjectPath);
            const result = await cleanupExpiredReviews(singleProjectPath, retentionDays, {
                reviewFileGateway,
                reviewLogFileGateway,
            });
            allDeletedFiles.push(...result.deletedReviewFiles, ...result.deletedLogFiles);
            totalDeletedCount += result.totalDeletedCount;
        }
        else {
            for (const repository of getRepositories()) {
                if (!repository.enabled)
                    continue;
                try {
                    const retentionDays = getProjectRetentionDays(repository.localPath);
                    const result = await cleanupExpiredReviews(repository.localPath, retentionDays, {
                        reviewFileGateway,
                        reviewLogFileGateway,
                    });
                    allDeletedFiles.push(...result.deletedReviewFiles, ...result.deletedLogFiles);
                    totalDeletedCount += result.totalDeletedCount;
                }
                catch (error) {
                    logger.error({ error, projectPath: repository.localPath }, 'Cleanup failed for repository');
                }
            }
        }
        return {
            success: true,
            deletedCount: totalDeletedCount,
            deletedFiles: allDeletedFiles,
        };
    });
};
//# sourceMappingURL=cleanup.routes.js.map