import { ApiError } from "../utils/ApiError.js";

// 404 handler
export function notFound(req, res, next) {
  next(new ApiError(404, `Route ${req.originalUrl} not found`));
}

// Centralized error handler
// eslint-disable-next-line no-unused-vars
export function errorHandler(err, req, res, next) {
  const status = err instanceof ApiError && err.statusCode ? err.statusCode : 500;
  const payload = {
    message: err.message || "Internal Server Error"
  };

  if (err instanceof ApiError && err.details) {
    payload.details = err.details;
  }

  if (process.env.NODE_ENV !== "production") {
    payload.stack = err.stack;
  }

  res.status(status).json(payload);
}

