import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { nameSchema } from "@/lib/validation/auth";

export async function GET(req: Request) {
  const { searchParams } = new URL(req.url);
  const nameRaw = searchParams.get("name") ?? "";
  const name = nameRaw.trim();

  const parsed = nameSchema.safeParse(name);
  if (!parsed.success) {
    const firstMsg = parsed.error.issues[0]?.message || "잘못된 닉네임 형식";
    return NextResponse.json(
      { error: { fieldErrors: { name: [firstMsg] } } },
      { status: 400 }
    );
  }

  const exists = await prisma.user.findFirst({ where: { name } });
  return NextResponse.json({ available: !exists });
}


