/**
 * GET /api/atlas?gpu=RTX+4060&vram_mb=8192
 *
 * Returns GPU x model performance data from the Hardware Performance Atlas.
 * No auth required — this is aggregated, anonymised public data.
 *
 * Query params (all optional):
 *   gpu      — partial GPU model name match (case-insensitive)
 *   vram_mb  — exact VRAM in MB filter
 *
 * Returns top 50 entries sorted by measurementCount descending.
 */

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

export async function GET(request: NextRequest) {
  const { searchParams } = new URL(request.url);
  const gpu = searchParams.get("gpu");
  const vramMb = searchParams.get("vram_mb");

  const where: Record<string, unknown> = {};
  if (gpu) where.gpuModel = { contains: gpu, mode: "insensitive" };
  if (vramMb) where.vramMb = parseInt(vramMb, 10);

  try {
    const entries = await db.atlasEntry.findMany({
      where,
      orderBy: { measurementCount: "desc" },
      take: 50,
    });

    return NextResponse.json(entries);
  } catch (err) {
    logger.error({ err }, "[atlas] DB query failed");
    return NextResponse.json(
      { error: "Failed to load atlas data" },
      { status: 500 },
    );
  }
}
