/**
 * GET /api/public-info/<resource>?refresh=1
 *
 * Server-side proxy + cache for files under the public-info repo. UI
 * never hits GitHub directly — avoids CORS / rate-limit headaches and
 * gives us a single cache shared across all browser tabs.
 *
 * Supported resources (extend by adding to RESOURCE_MAP):
 *   - `models` → `models/registry.json`
 *
 * The UI passes the short name (`models`), this route translates to the
 * actual path under the repo and pairs it with the right fallback.
 */

import { NextRequest, NextResponse } from 'next/server';
import { fetchPublicInfo } from '@/lib/public-info/fetch';
import { KNOWN_MODELS_FALLBACK } from '@/lib/agents/known-models';

interface ResourceSpec {
  path: string;
  fallback: unknown;
}

const RESOURCE_MAP: Record<string, ResourceSpec> = {
  models: { path: 'models/registry.json', fallback: KNOWN_MODELS_FALLBACK },
};

export async function GET(
  req: NextRequest,
  { params }: { params: Promise<{ resource: string }> },
): Promise<NextResponse> {
  const { resource } = await params;
  const spec = RESOURCE_MAP[resource];
  if (!spec) {
    return NextResponse.json({ error: `unknown resource: ${resource}` }, { status: 404 });
  }
  const forceRefresh = req.nextUrl.searchParams.get('refresh') === '1';
  const data = await fetchPublicInfo(spec.path, spec.fallback, { forceRefresh });
  return NextResponse.json(data);
}
