const { isObject } = require("lodash");

const validateCanvasInputMiddleware = (req, res, next) => {
  const { values, keys, override, w, h } = req.body;

  if (!Array.isArray(values) || !Array.isArray(keys)) {
    return res.status(400).send("Invalid input: 'values' and 'keys' must be arrays.");
  }

  if (values.some((value) => typeof value !== "number")) {
    return res.status(400).send("Invalid input: 'values' must be an array of numbers.");
  }

  if (keys.some((key) => typeof key !== "string")) {
    return res.status(400).send("Invalid input: 'keys' must be an array of strings.");
  }

  if (override && !isObject(override)) {
    return res.status(400).send("Override must be an object");
  }

  if (w && (!Number.isFinite(w) || w <= 0)) {
    return res.status(400).send("Width must be a positive number");
  }
  if (h && (!Number.isFinite(h) || h <= 0)) {
    return res.status(400).send("Height must be a positive number");
  }

  next(); // Proceed to the next middleware or route handler
};

module.exports = validateCanvasInputMiddleware;
