prepare( "SELECT id, name_ar, name_en, logo_url, cover_url, description_ar, city, address, latitude, longitude, category, min_order_amount, avg_prep_minutes, rating_avg, rating_count, is_open_override, working_hours FROM food_merchants WHERE id=? AND status='active' LIMIT 1" ); $st->execute([$merchantId]); $merchant = $st->fetch(); if (!$merchant) jsonError('Merchant not found', 404); $merchant['is_open'] = foodIsMerchantOpen($merchant); unset($merchant['working_hours'], $merchant['is_open_override']); $catSt = $food_con->prepare( "SELECT id, name_ar, name_en, sort_order FROM food_menu_categories WHERE merchant_id=? AND is_active=1 ORDER BY sort_order ASC" ); $catSt->execute([$merchantId]); $categories = $catSt->fetchAll(); $itemSt = $food_con->prepare( "SELECT id, category_id, name_ar, name_en, description_ar, image_url, price, is_available, prep_minutes, sort_order FROM food_menu_items WHERE merchant_id=? ORDER BY sort_order ASC" ); $itemSt->execute([$merchantId]); $items = $itemSt->fetchAll(); if ($items) { $itemIds = implode(',', array_map('intval', array_column($items, 'id'))); $optSt = $food_con->query( "SELECT id, item_id, group_name_ar, is_required, max_select, choices, sort_order FROM food_item_options WHERE item_id IN ($itemIds) ORDER BY sort_order ASC" ); $options = $optSt ? $optSt->fetchAll() : []; $byItem = []; foreach ($options as $o) { $o['choices'] = json_decode($o['choices'], true); $byItem[$o['item_id']][] = $o; } foreach ($items as &$it) { $it['options'] = $byItem[$it['id']] ?? []; } unset($it); } foreach ($categories as &$c) { $c['items'] = array_values(array_filter($items, fn($i) => (int)$i['category_id'] === (int)$c['id'])); } unset($c); jsonSuccess(['merchant' => $merchant, 'categories' => $categories]);