/**
 * Trust score computation for repositories.
 *
 * Produces a composite score in [0, 10] that reflects the credibility and
 * completeness of a repository's documentation.
 */

import type { Repository } from '$lib/types';

/**
 * Compute a trust score (0–10) for a repository.
 *
 * Score components:
 * - Stars        : up to 4 points on a log10 scale (10 k stars = 4 pts)
 * - Doc coverage : up to 3 points (500 snippets = 3 pts)
 * - Source type  : 1 point for GitHub repos
 * - Indexed state: 1 point when state is "indexed"
 * - Description  : 1 point when a description is present
 */
export function computeTrustScore(repo: Repository): number {
	let score = 0;

	// Stars (up to 4 points): log scale, 10k stars ≈ 4 pts.
	if (repo.stars) {
		score += Math.min(4, Math.log10(repo.stars + 1));
	}

	// Documentation coverage (up to 3 points).
	score += Math.min(3, (repo.totalSnippets ?? 0) / 500);

	// Source type (1 point for GitHub).
	if (repo.source === 'github') score += 1;

	// Successful indexing (1 point).
	if (repo.state === 'indexed') score += 1;

	// Has description (1 point).
	if (repo.description) score += 1;

	return Math.min(10, parseFloat(score.toFixed(1)));
}
