import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import type { Sql } from '../db.js';
import type { FleetState } from '../services/fleet-state.js';
import { computeRoutingScores, BADGE_LABELS } from '../services/routing-scores.js';

/**
 * P6.1: Advisory routing scores.
 *
 * GET /api/routing/scores — per (provider_id, model) advisory scores + badges.
 *   Surfaced as model-picker badges in BooChat. Advisory only; no enforcement.
 */
export function registerRoutingRoutes(
  app: FastifyInstance,
  sql: Sql,
  fleet: FleetState,
): void {
  app.get('/api/routing/scores', async (_req: FastifyRequest, reply: FastifyReply) => {
    const scores = await computeRoutingScores(sql, fleet);

    // Map of compositeId -> badge kinds, for cheap picker lookup.
    const badges: Record<string, string[]> = {};
    for (const s of scores) {
      if (s.badges.length > 0) badges[s.compositeId] = s.badges;
    }

    return reply.send({
      scores,
      badges,
      badgeLabels: BADGE_LABELS,
    });
  });
}
