import { NextRequest, NextResponse } from "next/server"
import { createUserWithPassword } from "@/lib/auth"
import { getUserByEmail } from "@/lib/db"

// One-time setup endpoint to create initial user
// Only works if no users exist yet or for the specific email
export async function POST(request: NextRequest) {
  try {
    const { email, password, name } = await request.json()

    if (!email || !password || !name) {
      return NextResponse.json({ error: "Email, password og navn er påkrævet" }, { status: 400 })
    }

    // Check if user already exists
    const existingUser = getUserByEmail(email)
    if (existingUser) {
      return NextResponse.json({ error: "Bruger findes allerede" }, { status: 409 })
    }

    const user = await createUserWithPassword(email, password, name)

    return NextResponse.json({
      success: true,
      user: { id: user.id, email: user.email, name: user.name },
    })
  } catch (error) {
    console.error("Setup error:", error)
    return NextResponse.json({ error: "Der opstod en fejl" }, { status: 500 })
  }
}
