import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_livestock/data/data_source/remote/livestock/livestock_remote.dart';
import 'package:rasadyar_livestock/data/model/response/address/address.dart';
import 'package:rasadyar_livestock/data/model/response/live_tmp/livestock_model.dart';

class LivestockRemoteDataSourceImp implements LivestockRemoteDataSource {
  @override
  Future<LocationDetails> getLocationDetailsByLatLng({
    required double latitude,
    required double longitude,
  }) async {
    try {
      Dio dio = Dio();
      dio.options.baseUrl = 'https://nominatim.openstreetmap.org/';
      dio.options.headers['User-Agent'] = 'RasadyarLivestock/2.0';
      final response = await dio.get(
        'reverse',
        queryParameters: {'lat': latitude, 'lon': longitude, 'format': 'json'},
      );
      if (response.statusCode == 200) {
        final data = response.data;
        return LocationDetails.fromJson(data);
      } else {
        throw Exception('Failed to load address');
      }
    } catch (e) {
      rethrow;
    }
  }

  @override
  Future<bool> createTaggingLiveStock({required LivestockData data}) async {
    try {
      Dio dio = Dio();
      dio.interceptors.add(PrettyDioLogger(
        requestBody: false,
        responseBody: false,
        requestHeader: true,
        responseHeader: true,
        error: true,
        compact: true,
      ));
      dio.options.baseUrl = 'https://everestacademy.ir/live/';
      final response = await dio.post('api.php', data: data.toJson());
      if (response.statusCode == 200) {
        final data = response.data;
        return true;
      } else {
        throw Exception('Failed to load address');
      }
    } catch (e) {
      rethrow;
    }
  }
}
