"error", "message" => "Missing parameters"]); exit; } $lat = (float)$lat; $lng = (float)$lng; $countryCode = strtoupper($country); if ($countryCode == 'JORDAN') $countryCode = 'JO'; if ($countryCode == 'SYRIA') $countryCode = 'SY'; if ($countryCode == 'EGYPT') $countryCode = 'EG'; $avgPricePerKm = 0; $topComp = 'TaxiF'; // Default try { $redis = getRedisConnection(); $cacheJson = $redis->get('siro:cache:pricing:grids'); if ($cacheJson) { $cacheData = json_decode($cacheJson, true); if ($cacheData && isset($cacheData['grids'])) { $gridSize = 0.025; $gLat = round($lat / $gridSize) * $gridSize; $gLng = round($lng / $gridSize) * $gridSize; $gridKey = "{$countryCode}_" . number_format($gLat, 3) . "_" . number_format($gLng, 3); $grids = $cacheData['grids']; if (isset($grids[$gridKey])) { $avgPricePerKm = (float)$grids[$gridKey]['avg_price']; $topComp = $grids[$gridKey]['top_competitor'] ?? 'TaxiF'; } else { $fallbackKey = "{$countryCode}_FALLBACK"; if (isset($grids[$fallbackKey])) { $avgPricePerKm = (float)$grids[$fallbackKey]['avg_price']; $topComp = $grids[$fallbackKey]['top_competitor'] ?? 'TaxiF'; } } } } } catch (Exception $e) { // Continue with defaults if Redis fails } // If we couldn't get a price from Redis, use a smart default based on country if ($avgPricePerKm <= 0) { if ($countryCode === 'JO') { $avgPricePerKm = 0.35; $topComp = 'TaxiF'; } else if ($countryCode === 'SY') { $avgPricePerKm = 4000; $topComp = 'Yango'; } else if ($countryCode === 'EG') { $avgPricePerKm = 15; $topComp = 'inDrive'; } } // Calculate the competitor's total price based on distance and average market per-km rate $competitorTotalPrice = round($distance * $avgPricePerKm, 2); // Make sure competitor is slightly higher than us for psychological effect // if their raw math somehow ended up lower due to straight per-km multiplication if ($siroPrice > 0 && $competitorTotalPrice <= $siroPrice) { // Force a dynamic difference based on country $multiplier = ($countryCode === 'JO') ? 1.15 : 1.10; $competitorTotalPrice = round($siroPrice * $multiplier, 2); } // Calculate savings $savingsPct = 0; if ($competitorTotalPrice > 0 && $siroPrice < $competitorTotalPrice) { $savingsPct = (($competitorTotalPrice - $siroPrice) / $competitorTotalPrice) * 100; } // Format the labels $compNameAr = match(strtolower($topComp)) { 'careem' => 'كريم', 'uber' => 'أوبر', 'yallago' => 'يلا غو', 'jenny' => 'جيني', 'taxif' => 'تكسي إف', 'com.taxif.passenger' => 'تكسي إف', 'indrive' => 'إن درايف', 'yango' => 'يانغو', 'petra' => 'بترا رايد', 'petra ride' => 'بترا رايد', 'gojo' => 'جو جو', 'didi' => 'ديدي', 'zakin' => 'زاكن', 'tofaddal'=> 'تفضل', default => $topComp }; $savingsLabel = "أوفر بـ " . number_format($savingsPct, 1) . "% من $compNameAr ⚡"; $siroCommissionRate = 0.14; // Default 14% commission if ($countryCode === 'JO') $siroCommissionRate = 0.14; $extraEarnings = $siroPrice * $siroCommissionRate; $driverExtraLabel = "رحلة مربحة! تكسب أكثر مقارنة بـ $compNameAr 💰"; // Return exactly what Dart expects in the root JSON echo json_encode([ "status" => "success", "has_competitor_data" => true, "competitor_avg_price" => $competitorTotalPrice, "top_competitor" => $topComp, "savings_percent" => $savingsPct, "savings_label" => $savingsLabel, "driver_extra_amount" => round($extraEarnings, 2), "driver_extra_label" => $driverExtraLabel ]); ?>