import 'package:dio/dio.dart';
import 'package:syncrow_web/pages/analytics/models/device_location_info.dart';
import 'package:syncrow_web/pages/analytics/params/get_device_location_data_param.dart';
import 'package:syncrow_web/pages/analytics/services/device_location/device_location_service.dart';

class DeviceLocationDetailsServiceDecorator implements DeviceLocationService {
  const DeviceLocationDetailsServiceDecorator(this._decoratee, this._dio);

  final DeviceLocationService _decoratee;
  final Dio _dio;

  @override
  Future<DeviceLocationInfo> get(GetDeviceLocationDataParam param) async {
    try {
      final deviceLocationInfo = await _decoratee.get(param);
      final response = await _dio.get<Map<String, dynamic>>(
        'reverse',
        queryParameters: {
          'format': 'json',
          'lat': 25.1880567,
          'lon': 55.266608,
        },
      );

      final data = response.data;
      if (data != null) {
        final addressData = data['address'] as Map<String, dynamic>;
        return deviceLocationInfo.copyWith(
          city: addressData['city'] as String?,
          country: addressData['country_code']?.toString().toUpperCase(),
          address: addressData['state'] as String?,
        );
      }

      return deviceLocationInfo;
    } catch (e) {
      throw Exception('Failed to load device location info: $e');
    }
  }
}
