import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:nearle/Models/summary/riderweeklykms.dart';
import 'package:nearle/views/helpers/constants/apiconstants.dart';


class RiderWeeklyKmController {
  final String baseUrl = ApiConstants.summaryriderkmLive;

  Future<Map<String, dynamic>> getRiderWeeklyKms(int userId) async {
    final url = Uri.parse("$baseUrl/getriderweeklykms?userid=$userId");
    final response = await http.get(url);

    if (response.statusCode == 200) {
      final body = json.decode(response.body);
      if (body['status'] == true) {
        final details = (body['details'] as List)
            .map((e) => RiderWeeklyKms.fromJson(e))
            .toList();

        return {
          'details': details,
          'total_kms': double.tryParse('${body['total_kms'] ?? 0}') ?? 0.0,
        };
      } else {
        throw Exception(body['message'] ?? "API returned false status");
      }
    } else {
      throw Exception("Failed to fetch (code: ${response.statusCode})");
    }
  }
}

