import { ReturnSuggestion } from '../models/return-suggestion';

/**
 * Type guard to check if an object is a valid ReturnSuggestion
 * @param value - The value to check
 * @returns True if the value is a ReturnSuggestion, false otherwise
 */
export const isReturnSuggestion = (
  value: unknown,
): value is ReturnSuggestion => {
  if (!value || typeof value !== 'object') {
    return false;
  }

  const suggestion = value as Partial<ReturnSuggestion>;

  // Check required properties from ReturnSuggestion
  return (
    // Check product exists and has required properties
    typeof suggestion.product === 'object' &&
    suggestion.product !== null &&
    (typeof suggestion.product.name === 'string' ||
      typeof suggestion.product.ean === 'string') &&
    // Check retailPrice exists and has required nested structure
    typeof suggestion.retailPrice === 'object' &&
    suggestion.retailPrice !== null &&
    typeof suggestion.retailPrice.value === 'object' &&
    suggestion.retailPrice.value !== null &&
    typeof suggestion.retailPrice.value.value === 'number' &&
    typeof suggestion.retailPrice.value.currency === 'string' &&
    // Check source exists and is a valid string
    typeof suggestion.source === 'string' &&
    suggestion.source.length > 0 &&
    // Check inherited ReturnSuggestionDTO properties (id is a number)
    typeof suggestion.id === 'number' &&
    // ReturnSuggestion-specific: Must have at least one distinguishing property
    ('accepted' in suggestion ||
      'rejected' in suggestion ||
      'sort' in suggestion) &&
    // Additionally, must NOT have ReturnItem-specific fields
    !('predefinedReturnQuantity' in suggestion) &&
    !('quantityReturned' in suggestion)
  );
};
