106 lines
3.3 KiB
Dart
106 lines
3.3 KiB
Dart
import 'package:uuid/uuid.dart';
|
|
import '../../../core/api/api_client.dart';
|
|
import 'models/trip_model.dart';
|
|
import 'models/stop_model.dart';
|
|
|
|
const _uuid = Uuid();
|
|
|
|
/// مستودع الرحلات — يُستخدم من TripBloc.
|
|
class TripRepository {
|
|
final ApiClient _api;
|
|
TripRepository(this._api);
|
|
|
|
/// GET /trips/mine — استرجاع رحلة نشطة.
|
|
Future<TripModel?> getActiveTrip() async {
|
|
try {
|
|
final r = await _api.dio.get('/trips/mine');
|
|
final data = r.data;
|
|
if (data == null) return null;
|
|
final items = (data['items'] ?? data) as List;
|
|
if (items.isEmpty) return null;
|
|
final trip = TripModel.fromJson(items.first as Map<String, dynamic>);
|
|
final active = ['searching', 'assigned', 'driver_arriving', 'driver_arrived', 'in_progress'];
|
|
return active.contains(trip.status) ? trip : null;
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// GET /trips/:id — جلب رحلة محددة.
|
|
Future<TripModel?> getTrip(String tripId) async {
|
|
try {
|
|
final r = await _api.dio.get('/trips/$tripId');
|
|
return TripModel.fromJson(r.data as Map<String, dynamic>);
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// POST /trips — إنشاء رحلة جديدة.
|
|
Future<TripModel> createTrip({
|
|
required double pickupLat,
|
|
required double pickupLng,
|
|
String? pickupAddress,
|
|
required double dropoffLat,
|
|
required double dropoffLng,
|
|
String? dropoffAddress,
|
|
List<StopModel>? stops,
|
|
required String vehicleClass,
|
|
String paymentMethod = 'cash',
|
|
String? couponCode,
|
|
String? scheduledAt,
|
|
}) async {
|
|
final data = <String, dynamic>{
|
|
'clientId': _uuid.v4(),
|
|
'pickup': {
|
|
'lat': pickupLat,
|
|
'lng': pickupLng,
|
|
if (pickupAddress != null) 'address': pickupAddress,
|
|
},
|
|
'dropoff': {
|
|
'lat': dropoffLat,
|
|
'lng': dropoffLng,
|
|
if (dropoffAddress != null) 'address': dropoffAddress,
|
|
},
|
|
'vehicleClass': vehicleClass,
|
|
'paymentMethod': paymentMethod,
|
|
};
|
|
if (stops != null && stops.isNotEmpty) {
|
|
data['stops'] = stops.map((s) => s.toJson()).toList();
|
|
}
|
|
if (couponCode != null && couponCode.isNotEmpty) {
|
|
data['couponCode'] = couponCode;
|
|
}
|
|
if (scheduledAt != null) data['scheduledAt'] = scheduledAt;
|
|
|
|
final r = await _api.dio.post('/trips', data: data);
|
|
return TripModel.fromJson(r.data as Map<String, dynamic>);
|
|
}
|
|
|
|
/// POST /trips/:id/cancel — إلغاء الرحلة.
|
|
Future<TripModel> cancelTrip(String tripId) async {
|
|
final r = await _api.dio.post('/trips/$tripId/cancel');
|
|
return TripModel.fromJson(r.data as Map<String, dynamic>);
|
|
}
|
|
|
|
/// POST /trips/:id/rate — تقييم الرحلة.
|
|
Future<void> rateTrip(String tripId, {required int stars, String? comment}) async {
|
|
await _api.dio.post('/trips/$tripId/rate', data: {
|
|
'stars': stars,
|
|
if (comment != null && comment.isNotEmpty) 'comment': comment,
|
|
});
|
|
}
|
|
|
|
/// GET /trips/rating/pending — هل هناك تقييم معلّق?
|
|
Future<Map<String, dynamic>?> getPendingRating() async {
|
|
try {
|
|
final r = await _api.dio.get('/trips/rating/pending');
|
|
final data = r.data as Map<String, dynamic>;
|
|
if (data['pending'] != null) return data;
|
|
return null;
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
}
|