import type { FastifyInstance } from 'fastify';
import type { Config } from '../config.js';
import type { ModelInfo } from '../types/api.js';

interface LlamaSwapModelsResponse {
  data?: ModelInfo[];
}

export function registerModelRoutes(app: FastifyInstance, config: Config): void {
  app.get('/api/models', async (_req, reply) => {
    try {
      const res = await fetch(`${config.LLAMA_SWAP_URL}/v1/models`);
      if (!res.ok) {
        reply.code(502);
        return { error: `llama-swap returned ${res.status}` };
      }
      const parsed = (await res.json()) as LlamaSwapModelsResponse;
      return parsed.data ?? [];
    } catch (err) {
      reply.code(502);
      return {
        error: 'failed to reach llama-swap',
        details: err instanceof Error ? err.message : String(err),
      };
    }
  });
}
