import { NextRequest, NextResponse } from "next/server";
import { requireSuperAdminAuth } from "@/lib/staff-auth";
import { prisma } from "@/lib/db";
// Convex routing (WU-13): auth domain needs a listUsers-by-role query before wiring.

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

    const staff = await prisma.users.findMany({
      where: {
        role: { in: ["STAFF", "ADMIN", "SUPER_ADMIN"] },
      },
      orderBy: { createdAt: "desc" },
      select: {
        id: true,
        name: true,
        email: true,
        username: true,
        role: true,
        isActive: true,
        lastLoginAt: true,
        createdAt: true,
      },
    });

    return NextResponse.json({ staff });
  } catch (error) {
    console.error("Failed to fetch staff:", error);
    return NextResponse.json(
      { error: "Failed to fetch staff" },
      { status: 500 },
    );
  }
}
