export const fallbackGoogleReviews = [
  {
    author_name: 'Anjali K.',
    text: 'Delicious dosa with authentic masala, the best in Waterloo! The staff is friendly and the flavors are spot on.',
    relative_time_description: '2 weeks ago',
    rating: 5,
  },
  {
    author_name: 'Priya S.',
    text: 'Cozy atmosphere and amazing sambar. The chutneys were fresh and the dosa was perfectly crispy.',
    relative_time_description: '1 month ago',
    rating: 5,
  },
  {
    author_name: 'Ravi M.',
    text: 'I love the authentic South Indian taste here. The service was fast and the food tasted homemade.',
    relative_time_description: '3 days ago',
    rating: 5,
  },
];

export function getReviewText(review) {
  return (
    review.text ||
    review.description ||
    review.snippet ||
    review.review_text ||
    review.body ||
    review.content ||
    ''
  );
}

export function getInitials(name) {
  return name
    .split(' ')
    .map((word) => word.charAt(0).toUpperCase())
    .slice(0, 2)
    .join('');
}

export function parseRating(review) {
  const rating = review.rating ?? review.review_rating ?? review.stars ?? 0;
  return Number(rating) || 0;
}

export function formatReviewsForHome(rawReviews) {
  return rawReviews
    .filter((review) => {
      const text = getReviewText(review);
      return text && text.trim().length > 0 && parseRating(review) >= 4;
    })
    .map((review) => {
      const text = getReviewText(review);
      const name = review.author_name || review.user?.name || 'Happy Customer';
      const rating = parseRating(review);
      const role = review.relative_time_description || review.time || 'Google Reviewer';

      return {
        text,
        name,
        role,
        initials: getInitials(name),
        rating,
      };
    });
}

export function formatReviewsForAbout(rawReviews) {
  return rawReviews
    .filter((review) => {
      const text = getReviewText(review);
      return text && text.trim().length > 0 && parseRating(review) >= 4;
    })
    .map((review) => ({
      quote: getReviewText(review),
      name: review.author_name || review.user?.name || 'Customer',
      role: review.relative_time_description || 'Google Review',
      rating: Math.round(parseRating(review) || 5),
    }));
}
