prepare( "SELECT id FROM transit_drivers WHERE main_driver_id=? AND status='active' LIMIT 1" ); $driverRow->execute([(string)$transit_user_id]); $driverRec = $driverRow->fetch(); if (!$driverRec) jsonError('Driver not found or not active', 403); $driverId = (int)$driverRec['id']; $st = $transit_con->prepare( "SELECT id, route_id, status FROM transit_trips WHERE id=? AND driver_id=? LIMIT 1" ); $st->execute([$tripId, $driverId]); $trip = $st->fetch(); if (!$trip) jsonError('Trip not found', 404); if ($trip['status'] !== 'started') jsonError('Trip is not in started state', 409); $transit_con->prepare( "UPDATE transit_trips SET status='completed', completed_at=NOW(), updated_at=NOW() WHERE id=?" )->execute([$tripId]); global $redis; if ($redis) $redis->del("transit:trip:{$tripId}:status"); // موقع الباص + ملكية الرحلة على Redis سيرفر الموقع transitClearTripOwner($tripId); global $redisLocation; if ($redisLocation) $redisLocation->del("transit:trip:{$tripId}:pos"); jsonSuccess(['trip_id' => $tripId, 'status' => 'completed'], 'Trip completed');