import 'dart:convert'; import 'package:http/http.dart' as http; /// Service in charge of dispatching SOS events to a backend endpoint. /// /// The backend is expected to accept POST JSON payloads like: /// { /// "tripId": "...", /// "lat": 0.0, /// "lng": 0.0, /// "message": "...", /// } class SosService { /// Base URL of the backend (e.g. https://api.example.com/sos). final String baseUrl; /// Optional API key header. final String? apiKey; /// Optional injected HTTP client (useful for testing). final http.Client _client; /// Creates a new SOS service. SosService({required this.baseUrl, this.apiKey, http.Client? client}) : _client = client ?? http.Client(); /// Sends an SOS event. Returns true on HTTP 200. Future sendSos({ required String tripId, required double lat, required double lng, String message = 'SOS déclenché', }) async { final uri = Uri.parse('$baseUrl/sos'); try { final response = await _client.post( uri, headers: { 'Content-Type': 'application/json', if (apiKey != null) 'Authorization': 'Bearer $apiKey', }, body: json.encode({ 'tripId': tripId, 'lat': lat, 'lng': lng, 'message': message, }), ); return response.statusCode == 200; } catch (_) { return false; } } }