// Shortcode: [google_menu_all]
add_shortcode('google_menu_all', function () {

  $cache_key = 'bali_eco_deli_menu_html_all';
  $cache_ttl = 12 * HOUR_IN_SECONDS;

  $cache_enabled = get_option('menu_schema_cache_enabled', '1') === '1';

  if ($cache_enabled) {
    $cached = get_transient($cache_key);
    if ($cached) return $cached;
  }

  $sheet_id = '1PeRiJk05I2MTqy5QPUE1jocGlqysM7M-WOBzz6LmE3U';

  // Read categories from __categories
  $index_url = 'https://docs.google.com/spreadsheets/d/' . $sheet_id . '/gviz/tq?tqx=out:json&sheet=' . urlencode('__categories');
  $index_response = wp_remote_get($index_url, ['timeout' => 8]);

  if (is_wp_error($index_response)) return '<!-- Categories fetch error -->';

  $index_body = wp_remote_retrieve_body($index_response);
  $index_json = trim(substr($index_body, strpos($index_body, '{'), strrpos($index_body, '}') - strpos($index_body, '{') + 1));
  $index_data = json_decode($index_json, true);

  if (!$index_data || empty($index_data['table']['rows'])) return '<!-- No categories found -->';

  $html = '<div class="gs-menu">';

  foreach ($index_data['table']['rows'] as $row) {

    if (empty($row['c'][0]['v'])) continue;
    $category = trim($row['c'][0]['v']);
    if (!$category) continue;

    // Fetch category sheet
    $url = 'https://docs.google.com/spreadsheets/d/' . $sheet_id . '/gviz/tq?tqx=out:json&sheet=' . urlencode($category);
    $response = wp_remote_get($url, ['timeout' => 8]);

    if (is_wp_error($response)) continue;

    $body = wp_remote_retrieve_body($response);
    $json = trim(substr($body, strpos($body, '{'), strrpos($body, '}') - strpos($body, '{') + 1));
    $data = json_decode($json, true);

    if (!$data || empty($data['table']['rows'])) continue;

    // A1 = Category description
    $category_desc = '';
    if (!empty($data['table']['rows'][0]['c'][0]['v'])) {
      $category_desc = trim($data['table']['rows'][0]['c'][0]['v']);
    }

    $html .= '<section class="gs-menu-section">';
    $html .= '<h2>' . esc_html($category) . '</h2>';

    if ($category_desc) {
      $html .= '<div class="gs-menu-category-desc">' . esc_html($category_desc) . '</div>';
    }

    $html .= '<ul class="gs-menu-list">';

    // Start from row 2 (index 1) for items
    foreach ($data['table']['rows'] as $i => $item_row) {

      if ($i === 0) continue; // skip description row

      if (empty($item_row['c'][0]['v'])) continue;
      $name = trim($item_row['c'][0]['v']);
      if (!$name) continue;

      $desc = !empty($item_row['c'][1]['v']) ? trim($item_row['c'][1]['v']) : '';

      $html .= '<li class="gs-menu-item">';
      $html .= '<button type="button" class="gs-menu-toggle" aria-expanded="false">';
      $html .= '<span class="gs-menu-name">' . esc_html($name) . '</span>';
      $html .= '<span class="gs-menu-icon">+</span>';
      $html .= '</button>';

      if ($desc) {
        $html .= '<div class="gs-menu-panel" hidden>';
        $html .= '<div class="gs-menu-desc">' . esc_html($desc) . '</div>';
        $html .= '</div>';
      }

      $html .= '</li>';
    }

    $html .= '</ul>';
    $html .= '</section>';
  }

  $html .= '</div>';

  if ($cache_enabled) {
    set_transient($cache_key, $html, $cache_ttl);
  }

  return $html;
});
