import { NextResponse } from "next/server";
import { logger } from "@/lib/logger";

/**
 * GET /api/cache/namespaces
 *
 * Proxies to llmhosts backend cache namespaces endpoint.
 */
export async function GET() {
  try {
    const backendUrl =
      process.env.LLMHOSTS_BACKEND_URL || "http://llmhosts:4000";

    const res = await fetch(`${backendUrl}/api/cache/namespaces`, {
      headers: { Accept: "application/json" },
    });

    if (!res.ok) {
      logger.error({ action: 'cache_namespaces_fetch', status: res.status, statusText: res.statusText }, 'Failed to fetch cache namespaces');
      return NextResponse.json(
        { namespaces: [], total: 0 },
        { status: 200 }
      );
    }

    const data = await res.json();
    return NextResponse.json(data);
  } catch (error) {
    logger.error({ err: error, action: 'cache_namespaces_fetch' }, 'Error fetching cache namespaces');
    return NextResponse.json(
      { namespaces: [], total: 0 },
      { status: 200 }
    );
  }
}
