/**
 * `GET /v1/models` — OpenAI-shaped list of configured backends.
 *
 * Returns one entry per `gateway.backends[]` with `id` set to the backend
 * id (which is also what `x-aao-backend-id` and `x-litellm-model-id` will
 * carry in responses). Clients hit this endpoint to discover which model
 * names they may request.
 *
 * Auth required: the response surfaces backend ids which could otherwise
 * be enumerated by an unauthenticated probe.
 */
import type { Request, Response } from 'express';
import type { GatewayBackendConfig } from './config.js';

export interface ModelsEndpointDeps {
  getBackends: () => GatewayBackendConfig[];
}

export interface OpenAIModelEntry {
  id: string;
  object: 'model';
  owned_by: 'aao-gateway';
}

export interface OpenAIModelsResponse {
  object: 'list';
  data: OpenAIModelEntry[];
}

export function buildModelsResponse(backends: GatewayBackendConfig[]): OpenAIModelsResponse {
  const data: OpenAIModelEntry[] = backends.map(b => ({
    id: b.id,
    object: 'model',
    owned_by: 'aao-gateway',
  }));
  return { object: 'list', data };
}

export function buildModelsHandler(deps: ModelsEndpointDeps) {
  return function modelsHandler(_req: Request, res: Response): void {
    res.json(buildModelsResponse(deps.getBackends()));
  };
}
