import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:miler/Models/summary/riderweeklykms.dart'; import 'package:miler/data/miler_api.dart'; import 'package:miler/views/helpers/constants/apiconstants.dart'; class RiderWeeklyKmController { final String baseUrl = ApiConstants.summaryriderkmLive; /// `GET /miler/earnings?period=weekly` → `data:{ total_kms, breakdown }`. /// /// [userId] is unused: the token identifies the rider, and asking for someone /// else's kilometres is not a thing the endpoint offers. Kept on the /// signature because the provider above passes it and the call sites read /// better for naming whose distance they mean. Future> getRiderWeeklyKms(int userId) async { final res = await MilerApi.earnings(period: 'weekly'); if (!res.ok) { throw Exception('Failed to fetch (code: ${res.status})'); } final breakdown = res.map['breakdown']; final details = (breakdown is List ? breakdown : const []) .whereType() .map((e) => RiderWeeklyKms.fromJson(Map.from(e))) .toList(); return { 'details': details, 'total_kms': (res.map['total_kms'] ?? 0).toDouble(), }; } }