import { NextApiResponse } from 'next';
import { AuthenticatedRequest } from '../../../../lib/middleware/authMiddleware';
import { requireAdmin } from '../../../../lib/middleware/adminMiddleware';
import { UserService } from '../../../../backend/services/userService';

export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
    if (req.method !== 'GET') {
        return res.status(405).json({ success: false, error: 'Method not allowed' });
    }

    // Require admin authentication
    const isAdmin = await requireAdmin(req, res);
    if (!isAdmin) {
        return;
    }

    try {
        const users = await UserService.getAllUsers();
        return res.status(200).json({ success: true, data: users });
    } catch (error) {
        console.error('Error fetching users:', error);
        return res.status(500).json({ success: false, error: 'Failed to fetch users' });
    }
}
