import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { registerSchema } from "@/lib/validation/auth";
import { hashPassword } from "@/lib/password";

export async function POST(req: Request) {
  const body = await req.json();
  const parsed = registerSchema.safeParse(body);
  if (!parsed.success)
    return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
  const { nickname, name, phone, birth, password } = parsed.data;
  const exists = await prisma.user.findFirst({ where: { OR: [{ nickname }, { phone }] } });
  if (exists) return NextResponse.json({ error: "이미 존재하는 사용자" }, { status: 409 });
  const user = await prisma.user.create({
    data: {
      nickname,
      name,
      phone,
      birth: new Date(birth),
      passwordHash: hashPassword(password),
      agreementTermsAt: new Date(),
    },
    select: { userId: true, nickname: true },
  });
  return NextResponse.json({ user }, { status: 201 });
}


