Files
Siro/backend/ride/helpCenter/ask_gemini.php
T

110 lines
3.9 KiB
PHP

<?php
require_once __DIR__ . '/../../connect.php';
require_once __DIR__ . '/../../core/Services/SiroGeminiService.php';
require_once __DIR__ . '/../../encrypt_decrypt.php';
// Variables from connect.php (JWT token)
global $user_id, $role, $con;
if (!$user_id) {
jsonError("Unauthorized");
exit;
}
$driverID = $user_id;
$helpQuestion = filterRequest("helpQuestion");
if (empty($helpQuestion)) {
jsonError("Missing parameters");
exit;
}
try {
global $encryptionHelper;
// 1. Fetch driver info (Decrypting names and phone)
$stmt = $con->prepare("SELECT `first_name`, `last_name`, `phone` FROM `driver` WHERE `id` = :id");
$stmt->bindParam(':id', $driverID);
$stmt->execute();
$driverRow = $stmt->fetch(PDO::FETCH_ASSOC);
$driverInfo = [];
if ($driverRow) {
$driverInfo = [
'first_name' => $driverRow['first_name'] ? $encryptionHelper->decryptData($driverRow['first_name']) : '',
'last_name' => $driverRow['last_name'] ? $encryptionHelper->decryptData($driverRow['last_name']) : '',
'phone' => $driverRow['phone'] ? $encryptionHelper->decryptData($driverRow['phone']) : '',
'user_type' => $role
];
}
// 2. Fetch wallet balance using S2S
$walletServer = getenv('PAYMENT_SERVER_URL') ?: 'https://wallet.siromove.com';
$walletUrl = "$walletServer/v2/main/ride/driverWallet/get_s2s_wallet_dashboard.php";
$ch = curl_init($walletUrl);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(["driverID" => $driverID]),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'X-S2S-Api-Key: ' . getenv('S2S_SHARED_KEY')
]
]);
$s2sRes = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$totalWallet = 0.0;
if ($httpCode === 200 && $s2sRes) {
$resDecoded = json_decode($s2sRes, true);
if ($resDecoded && isset($resDecoded['status']) && $resDecoded['status'] === 'success') {
$totalWallet = (float)($resDecoded['message']['totalWallet'] ?? 0.0);
}
}
$walletInfo = ['balance' => $totalWallet];
// 3. Fetch recent trips (Correct table is `ride` and correct columns)
$recentTrips = [];
try {
$stmt3 = $con->prepare("SELECT `id`, `date`, `time`, `status`, `price`, `paymentMethod` FROM `ride` WHERE `driver_id` = :id ORDER BY `id` DESC LIMIT 3");
$stmt3->bindParam(':id', $driverID);
$stmt3->execute();
$recentTrips = $stmt3->fetchAll(PDO::FETCH_ASSOC) ?: [];
} catch (PDOException $e) {
// Ignored
}
$contextData = [
'driver_info' => $driverInfo,
'wallet_balance' => $walletInfo,
'recent_trips' => $recentTrips,
];
// 4. Call Gemini
$geminiService = new SiroGeminiService();
$aiReply = $geminiService->answerSupportInquiry($helpQuestion, $contextData);
if (!$aiReply) {
$aiReply = "مرحباً كابتن، استلمنا استفسارك ولكن المساعد الذكي غير متاح حالياً. سيقوم فريق الدعم بمراجعة رسالتك قريباً.";
}
// 5. Insert into helpCenter
$sql = "INSERT INTO `helpCenter` (`driverID`, `helpQuestion`, `replay`) VALUES (:driverID, :helpQuestion, :replay)";
$stmt4 = $con->prepare($sql);
$stmt4->bindParam(':driverID', $driverID);
$stmt4->bindParam(':helpQuestion', $helpQuestion);
$stmt4->bindParam(':replay', $aiReply);
$stmt4->execute();
if ($stmt4->rowCount() > 0) {
jsonSuccess(null, "Help question saved and answered");
} else {
jsonError("Failed to save help question");
}
} catch (Exception $e) {
jsonError("An error occurred while processing your request.");
}
?>