import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;

class CurrencyService {
  Future<Map<String, dynamic>> fetchCurrencies() async {
    final uri = Uri.parse('https://openexchangerates.org/api/currencies.json');
    debugPrint("Fetching currencies from: $uri");

    try {
      final response = await http.get(uri);

      if (response.statusCode == 200) {
        return response.body.isNotEmpty ? jsonDecode(response.body) : {};
      } else {
        throw Exception('Failed to load currencies. Status: ${response.statusCode}');
      }
    } on SocketException {
      throw Exception('No Internet connection.');
    } catch (e) {
      debugPrint('Error fetching currencies: $e');
      rethrow;
    }
  }
}
