import { ReturnItem } from '../models/return-item';

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

  const item = value as Partial<ReturnItem>;

  // Check required properties from ReturnItem
  return (
    // Check product exists and has required properties
    typeof item.product === 'object' &&
    item.product !== null &&
    (typeof item.product.name === 'string' ||
      typeof item.product.ean === 'string') &&
    // Check retailPrice exists and has required nested structure
    typeof item.retailPrice === 'object' &&
    item.retailPrice !== null &&
    typeof item.retailPrice.value === 'object' &&
    item.retailPrice.value !== null &&
    typeof item.retailPrice.value.value === 'number' &&
    typeof item.retailPrice.value.currency === 'string' &&
    // Check source exists and is a valid string
    typeof item.source === 'string' &&
    item.source.length > 0 &&
    // Check inherited ReturnItemDTO properties (id is a number)
    typeof item.id === 'number' &&
    // ReturnItem-specific: Must NOT have ReturnSuggestion-specific fields
    !('accepted' in item) &&
    !('rejected' in item) &&
    !('sort' in item) &&
    // ReturnItem can have predefinedReturnQuantity, quantityReturned, or neither
    // If it has none of the ReturnSuggestion-specific fields, it's a ReturnItem
    true
  );
};
