import { NextRequest, NextResponse } from "next/server" import { cookies } from "next/headers" export async function POST(req: NextRequest) { const { email, password, name, user_type, plan } = await req.json() if (!email || !password) { return NextResponse.json({ error: "Email and password required" }, { status: 400 }) } try { const apiUrl = process.env.API_URL || "http://localhost:8000" const res = await fetch(`${apiUrl}/autojobs/api/users`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, name, user_type: user_type || "private", plan: plan || "free" }) }) if (!res.ok) { return NextResponse.json({ error: "Failed to create user" }, { status: 500 }) } const data = await res.json() const cookieStore = await cookies() cookieStore.set("autojobs_user", email, { httpOnly: true, secure: process.env.NODE_ENV === "production", sameSite: "lax", maxAge: 60 * 60 * 24 * 30 }) // Also store user_id for checkout if (data.id) { cookieStore.set("autojobs_user_id", String(data.id), { httpOnly: true, secure: process.env.NODE_ENV === "production", sameSite: "lax", maxAge: 60 * 60 * 24 * 30 }) } return NextResponse.json({ status: "ok", user_id: data.id }) } catch { return NextResponse.json({ error: "Server error" }, { status: 500 }) } }