import { NextRequest, NextResponse } from 'next/server';
import { withAuth } from '@/lib/api-guards';

// GET: Fetch all users in user's scope (restricted to ADMIN and DIRECTOR)
export const GET = withAuth(async (req, { prisma }) => {
  try {
    const users = await prisma.user.findMany({
      select: {
        id: true,
        username: true,
        email: true,
        role: true,
        hotelId: true,
        area: true,
        status: true,
        hotel: {
          select: {
            name: true,
            code: true,
          }
        }
      },
      orderBy: { username: 'asc' }
    });

    return NextResponse.json({ users });
  } catch (err: any) {
    console.error('Failed to fetch users:', err);
    return NextResponse.json({ error: 'Failed to fetch users' }, { status: 500 });
  }
}, ['admin', 'director']);
