/**
 * Safely convert an unknown value to a finite number.
 * Returns 0 when the value is not a finite number.
 *
 * Used to normalize numeric columns returned by Drizzle (which may be
 * string | number depending on the driver) into plain numbers.
 */
export const toNumber = (v: unknown): number => {
  const n = typeof v === "number" ? v : Number(v)
  return Number.isFinite(n) ? n : 0
}

/**
 * Normalize a raw score to a 0-100 scale based on its full score.
 * Returns 0 when fullScore is non-positive. Result is rounded to 2 decimals.
 */
export const normalize = (score: number, fullScore: number): number => {
  if (fullScore <= 0) return 0
  return Math.round((score / fullScore) * 10000) / 100
}
