/**
 * Error message templates for XSS validation failures
 * Provides clear, actionable error messages to users
 */

export const ValidationErrorMessages = {
  // File name validation errors
  fileName: {
    suspicious: (fileName: string) =>
      `File name contains suspicious characters or patterns: "${fileName}". Please rename the file to remove special characters like <, >, :, ", /, \\, |, ?, *.`,
    nullBytes: 'File name contains null bytes. Please rename the file.',
    tooLong: (length: number) =>
      `File name is too long (${length} characters). Maximum allowed is 255 characters. Please rename the file.`,
  },

  // File extension validation errors
  extension: {
    dangerous: (ext: string) =>
      `File extension ${ext} is not allowed for security reasons. This file type could pose a security risk.`,
    notAllowed: (ext: string, allowed: string[]) =>
      `File extension ${ext} is not allowed. Allowed types: ${allowed.join(', ')}. Please select a file with one of the allowed extensions.`,
  },

  // MIME type validation errors
  mimeType: {
    notAllowed: (mimeType: string, allowed: string[]) =>
      `File type ${mimeType} is not allowed. Allowed types: ${allowed.join(', ')}. Please select a file with one of the allowed types.`,
    mismatch: (declared: string, detected: string) =>
      `File type verification failed. The file content does not match its declared type (declared: ${declared}, detected: ${detected}). Please ensure the file matches its extension.`,
    undetectable: 'Unable to detect file type from buffer. The file may be corrupted. Please try uploading a different file.',
  },

  // File size validation errors
  size: {
    tooLarge: (sizeMB: number, maxMB: number) =>
      `File size (${sizeMB.toFixed(2)}MB) exceeds maximum allowed size (${maxMB}MB). Please select a smaller file.`,
  },

  // Server-side validation errors
  server: {
    mimeTypeMismatch: (declared: string, detected: string) =>
      `File type verification failed. The file content does not match its declared type. Please ensure the file matches its extension.`,
    disallowedType: (mimeType: string) =>
      `File type ${mimeType} is not allowed for this upload context. Please select a file with an allowed type.`,
    detectionFailed: 'Unable to verify file type. Please try again or contact support.',
    uploadFailed: 'File upload failed validation. Please try uploading a different file.',
  },

  // Generic errors
  generic: {
    noFile: 'No file provided. Please select a file to upload.',
    validationFailed: 'File validation failed. Please check the file and try again.',
    unknown: 'An unexpected error occurred during file validation. Please try again.',
  },
};

/**
 * Get user-friendly error message from validation error
 * @param error - Error message or validation result
 * @returns User-friendly error message
 */
export function getUserFriendlyErrorMessage(error: string | undefined): string {
  if (!error) {
    return ValidationErrorMessages.generic.validationFailed;
  }

  // Return the error as-is if it's already a user-friendly message
  // (it will be one of the messages from ValidationErrorMessages)
  return error;
}

/**
 * Get corrective action suggestion based on error type
 * @param error - Error message
 * @returns Suggested corrective action
 */
export function getCorrectiveAction(error: string | undefined): string {
  if (!error) {
    return 'Please check the file and try again.';
  }

  if (error.includes('suspicious characters')) {
    return 'Rename the file to remove special characters.';
  }

  if (error.includes('not allowed')) {
    return 'Select a file with an allowed type.';
  }

  if (error.includes('too long')) {
    return 'Rename the file to make it shorter.';
  }

  if (error.includes('exceeds maximum')) {
    return 'Select a smaller file.';
  }

  if (error.includes('type verification failed')) {
    return 'Ensure the file matches its extension.';
  }

  if (error.includes('corrupted')) {
    return 'Try uploading a different file.';
  }

  return 'Please try again or contact support.';
}
