const { body, validationResult } = require("express-validator");

// URL validation handled via express-validator isURL

const validateProfile = [
  body("bio")
    .optional()
    .isLength({ max: 500 })
    .withMessage("Bio cannot exceed 500 characters."),
  body("location").optional().isString(),
  body("website")
    .optional()
    .if(body("website").notEmpty())
    .isURL({ protocols: ["http", "https", "ftp"], require_protocol: true })
    .withMessage("Invalid website URL."),
  body("social.twitter")
    .optional()
    .if(body("social.twitter").notEmpty())
    .isURL({ protocols: ["http", "https", "ftp"], require_protocol: true })
    .withMessage("Invalid Twitter URL."),
  body("social.github")
    .optional()
    .if(body("social.github").notEmpty())
    .isURL({ protocols: ["http", "https", "ftp"], require_protocol: true })
    .withMessage("Invalid Github URL."),
  body("social.linkedin")
    .optional()
    .if(body("social.linkedin").notEmpty())
    .isURL({ protocols: ["http", "https", "ftp"], require_protocol: true })
    .withMessage("Invalid LinkedIn URL."),
  body("privacySettings.profileVisibility")
    .optional()
    .isIn(["public", "private"])
    .withMessage("Profile visibility must be public or private."),
  body("preferences.emailNotifications").optional().isBoolean(),
  body("preferences.pushNotifications").optional().isBoolean(),
  body("preferences.theme")
    .optional()
    .isIn(["light", "dark"])
    .withMessage("Theme must be light or dark."),

  (req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }
    next();
  },
];

module.exports = { validateProfile };

