import { HTTP_STATUS } from "../constants/index.js";
import { errorResponse } from "../utils/response.js";
import logger from "../utils/logger.js";

export const notFoundHandler = (req, res) => {
  return errorResponse(res, `Route ${req.method} ${req.path} not found`, HTTP_STATUS.NOT_FOUND);
};

export const errorHandler = (err, req, res, next) => {
  logger.error("Error handler:", err);
  logger.error("Error details:", {
    message: err?.message,
    stack: err?.stack,
    name: err?.name,
    code: err?.code
  });
  
  if (res.headersSent) {
    return next(err);
  }

  const statusCode = err.statusCode || HTTP_STATUS.INTERNAL_SERVER_ERROR;
  const message = err.message || "Internal server error";
  
  return errorResponse(res, message, statusCode);
};



