import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:inventory_monitor_app/config/api_config.dart'; import 'package:inventory_monitor_app/models/dashboard_models.dart'; import 'package:inventory_monitor_app/models/device.dart'; import 'package:url_launcher/url_launcher.dart'; class ApiService { Future getDashboardSummary() async { final response = await http.get(Uri.parse('$apiBaseUrl/api/dashboard/summary')); if (response.statusCode == 200) { return DashboardSummary.fromJson(json.decode(response.body)); } else { throw Exception('Failed to load dashboard summary'); } } Future> getOsDistribution() async { final response = await http.get(Uri.parse('$apiBaseUrl/api/dashboard/os-distribution')); if (response.statusCode == 200) { final List data = json.decode(response.body); return data .map((item) => ChartDataPoint.fromJson(item, labelKey: 'osVersion', countKey: 'count')) .toList(); } else { throw Exception('Failed to load OS distribution'); } } Future> getStorageByType() async { final response = await http.get(Uri.parse('$apiBaseUrl/api/dashboard/storage-by-type')); if (response.statusCode == 200) { final data = json.decode(response.body); // Convert bytes to Terabytes for display const toTb = 1 / (1024 * 1024 * 1024 * 1024); return { 'ssd': (data['totalSsdBytes'] as num).toDouble() * toTb, 'hdd': (data['totalHddBytes'] as num).toDouble() * toTb, }; } else { throw Exception('Failed to load storage data'); } } Future> getTopAdminAccounts() async { final response = await http.get(Uri.parse('$apiBaseUrl/api/dashboard/admin-accounts')); if (response.statusCode == 200) { final List data = json.decode(response.body); return data .map((item) => ChartDataPoint.fromJson(item, labelKey: 'accountName', countKey: 'count')) .toList(); } else { throw Exception('Failed to load admin accounts'); } } Future> getDevices({ int page = 1, int pageSize = 10, String? sortBy, bool sortAscending = true, String? deviceType, String? processor, String? ram, String? location, }) async { final queryParams = { 'page': page.toString(), 'pageSize': pageSize.toString(), 'sortAscending': sortAscending.toString(), if (sortBy != null) 'sortBy': sortBy, if (deviceType != null && deviceType.isNotEmpty) 'deviceType': deviceType, if (processor != null && processor.isNotEmpty) 'processor': processor, if (ram != null && ram.isNotEmpty) 'ram': ram, if (location != null && location.isNotEmpty) 'location': location, }; final uri = Uri.parse('$apiBaseUrl/api/devices') .replace(queryParameters: queryParams); final response = await http.get(uri); if (response.statusCode == 200) { return PaginatedResult.fromJson( json.decode(response.body), (json) => Device.fromJson(json)); } else { throw Exception('Failed to load devices'); } } Future getDeviceById(int id) async { final response = await http.get(Uri.parse('$apiBaseUrl/api/devices/$id')); if (response.statusCode == 200) { return Device.fromJson(json.decode(response.body)); } else { throw Exception('Failed to load device details'); } } void exportDevices(String format, {String? deviceType, String? processor, String? ram, String? location}) { final queryParams = { if (deviceType != null && deviceType.isNotEmpty) 'deviceType': deviceType, if (processor != null && processor.isNotEmpty) 'processor': processor, if (ram != null && ram.isNotEmpty) 'ram': ram, if (location != null && location.isNotEmpty) 'location': location, }; final uri = Uri.parse('$apiBaseUrl/api/export/$format') .replace(queryParameters: queryParams); // Use url_launcher to open the download URL in the browser launchUrl(uri); } }