
const tools = require('../tools/general');
const server_constants = require('../tools/server_constants');

/**
 * Validate registration data
 */
function validateRegistration(body) {
  const { username, email, password, name, surname } = body;

  // Check required fields
  if (!username || !email || !password) {
    return {
      code: server_constants.RIS_CODE_ERR,
      message: 'Campi obbligatori mancanti'
    };
  }

  // Check minimum lengths
  if (email.length < 6) {
    return {
      code: server_constants.RIS_CODE_ERR,
      message: 'Email troppo corta'
    };
  }

  if (username.length < 4) {
    return {
      code: server_constants.RIS_CODE_USERNAME_NOT_VALID,
      message: 'Username deve essere almeno 4 caratteri'
    };
  }

  if (password.length < 5) {
    return {
      code: server_constants.RIS_CODE_ERR,
      message: 'Password deve essere almeno 5 caratteri'
    };
  }

  // Check username format
  if (!tools.isAlphaNumericAndSpecialCharacter(username)) {
    return {
      code: server_constants.RIS_CODE_USERNAME_NOT_VALID,
      message: 'Username contiene caratteri non validi'
    };
  }

  // Check for blocked words
  if (tools.blockwords(username) || tools.blockwords(name) || tools.blockwords(surname)) {
    return {
      code: server_constants.RIS_CODE_ERR,
      message: 'Contenuto non valido'
    };
  }

  return null;
}

/**
 * Validate login data
 */
function validateLogin(body) {
  const { username, password, idapp } = body;

  // Check required fields
  if (!username || !password || !idapp) {
    return {
      code: server_constants.RIS_CODE_ERR,
      message: 'Campi obbligatori mancanti'
    };
  }

  // Check minimum lengths
  if (username.length < 3) {
    return {
      code: server_constants.RIS_CODE_LOGIN_ERR,
      message: 'Username troppo corto'
    };
  }

  if (password.length < 4) {
    return {
      code: server_constants.RIS_CODE_LOGIN_ERR,
      message: 'Password troppo corta'
    };
  }

  return null;
}

/**
 * Validate email format
 */
function validateEmail(email) {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return emailRegex.test(email);
}

/**
 * Validate phone number format
 */
function validatePhoneNumber(phone) {
  if (!phone) return true; // Optional field
  
  // Remove spaces and special characters
  const cleanPhone = phone.replace(/[\s\-\(\)]/g, '');
  
  // Check if it's numeric and reasonable length
  return /^\+?\d{8,15}$/.test(cleanPhone);
}

/**
 * Validate user profile data
 */
function validateProfileData(profileData) {
  const errors = [];

  if (profileData.email && !validateEmail(profileData.email)) {
    errors.push('Email non valida');
  }

  if (profileData.cell && !validatePhoneNumber(profileData.cell)) {
    errors.push('Numero di telefono non valido');
  }

  if (profileData.username) {
    if (profileData.username.length < 4) {
      errors.push('Username deve essere almeno 4 caratteri');
    }
    if (!tools.isAlphaNumericAndSpecialCharacter(profileData.username)) {
      errors.push('Username contiene caratteri non validi');
    }
  }

  return errors.length > 0 ? {
    code: server_constants.RIS_CODE_ERR,
    message: errors.join(', ')
  } : null;
}

/**
 * Validate permissions value
 */
function validatePermissions(perm) {
  const validPermissions = [0, 1, 2, 3, 4, 5]; // Based on your Perm enum
  return validPermissions.includes(perm);
}

/**
 * Validate circuit/group command
 */
function validateCommand(cmd, validCommands) {
  return validCommands.includes(cmd);
}

/**
 * Sanitize user input (remove dangerous characters)
 */
function sanitizeInput(input) {
  if (typeof input !== 'string') return input;
  
  return input
    .trim()
    .replace(/[<>]/g, '') // Remove potential XSS
    .replace(/['";]/g, ''); // Remove SQL injection chars
}

/**
 * Validate MongoDB ObjectId
 */
function validateObjectId(id) {
  return /^[0-9a-fA-F]{24}$/.test(id);
}

module.exports = {
  validateRegistration,
  validateLogin,
  validateEmail,
  validatePhoneNumber,
  validateProfileData,
  validatePermissions,
  validateCommand,
  sanitizeInput,
  validateObjectId
};