/**
 * @copyright 2026 NHCarrigan
 * @license Naomi's Public License
 * @author Naomi Carrigan
 */

import { FastifyReply, FastifyRequest } from "fastify";
import { UserService } from "../services/user.service";

const userService = new UserService();

/**
 * Middleware to check if the authenticated user is banned.
 * Must be used after app.authenticate.
 */
export async function bannedGuard(
  request: FastifyRequest,
  reply: FastifyReply
): Promise<void> {
  const user = request.user as { id: string };

  if (!user?.id) {
    return reply.code(401).send({ error: "Unauthorized" });
  }

  const isBanned = await userService.isUserBanned(user.id);
  if (isBanned) {
    return reply.code(403).send({ error: "You have been banned" });
  }
}
