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

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

  const parsed = nicknameSchema.safeParse(nickname);
  if (!parsed.success) {
    const firstMsg = parsed.error.issues[0]?.message || "잘못된 아이디 형식";
    return NextResponse.json(
      { error: { fieldErrors: { nickname: [firstMsg] } } },
      { status: 400 }
    );
  }

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


