// Google Reviews Badge Shortcode
add_shortcode('google_reviews_badge', function () {

    $cache_key = 'bali_eco_deli_google_rating';
    $cache_ttl = 24 * HOUR_IN_SECONDS;

    $data = get_transient($cache_key);

    if (!$data) {
        $api_key  = 'AIzaSyD0OQGH5Sl8rPcdsDApb_vMyjO68hvh0xY';
        $place_id = 'ChIJtyxZygpt0i0RnOnelbdsHh0';

        if (!$api_key || !$place_id) return '';

        $url = add_query_arg([
            'place_id' => $place_id,
            'fields'   => 'rating,user_ratings_total',
            'key'      => $api_key
        ], 'https://maps.googleapis.com/maps/api/place/details/json');

        $response = wp_remote_get($url, ['timeout' => 8]);
        if (is_wp_error($response)) return '';

        $body = json_decode(wp_remote_retrieve_body($response), true);
        if (empty($body['result']['rating'])) return '';

        $data = [
            'ratingValue' => $body['result']['rating'],
            'reviewCount' => $body['result']['user_ratings_total']
        ];

        set_transient($cache_key, $data, $cache_ttl);
    }

    if (empty($data['ratingValue']) || empty($data['reviewCount'])) return '';

    $rating = number_format((float) $data['ratingValue'], 1);
    $count  = number_format((int) $data['reviewCount']);

return '
<div class="bed-reviews-badge">
  <span class="bed-reviews-rating">★ ' . $rating . '</span>
  <span class="bed-reviews-count">' . $count . '+ Reviews</span>
  <span class="bed-reviews-location">• Near Jungut Batu Beach</span>
</div>';
});
