import { NextResponse } from "next/server"
import { getSessionUser } from "@/lib/auth"
import { query } from "@/lib/db"

export async function GET() {
  try {
    const user = await getSessionUser()
    if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })

    const result = await query(
      `SELECT u.id, u.first_name, u.last_name, u.email
       FROM users u
       WHERE u.deleted_at IS NULL AND u.id != $1
       ORDER BY u.first_name ASC`,
      [user.id],
    )

    const users = result.rows.map((r: any) => ({
      id: r.id,
      name: `${r.first_name} ${r.last_name}`,
      email: r.email,
    }))

    return NextResponse.json({ users })
  } catch (error) {
    console.error("Event users error:", error)
    return NextResponse.json({ error: "Failed to load users" }, { status: 500 })
  }
}
