import { AuthUser } from "@/contexts/auth/domain";
import { generateExpressError } from "@/contexts/common/infrastructure/express";
import { NextFunction, Request, Response } from "express";
import httpStatus from "http-status";

interface AuthenticatedRequest extends Request {
  user?: AuthUser;
}

const profileMiddleware = (req: Request, res: Response, next: NextFunction) => {
  const _req = req as AuthenticatedRequest;
  const user = <AuthUser>_req.user;

  if (!user || !user.isAdmin) {
    generateExpressError(req, res, httpStatus.UNAUTHORIZED);
  }
  next();
};
