
using Domain.Entities;
using System.Net.Http.Json;

namespace phronCare.UIBlazor.Services.Integrations
{
    public class ExchangeRateService: IExchangeRateService

    {
        private readonly HttpClient _http;

        public ExchangeRateService(HttpClient http)
        {
            _http = http;
        }

        public async Task<EExchangeRateHistory> GetYesterdayRateAsync()
        {
            // Ajustá la URL si tu API está en un path o puerto distinto
            var dto = await _http.GetFromJsonAsync<EExchangeRateHistory>("api/ExchangeRate/yesterday");
            if (dto == null)
                throw new InvalidOperationException("No se obtuvo la cotización de ayer.");
            return dto;
        }

        public async Task<EExchangeRateHistory?> GetByDateAsync(DateOnly date)
        {
            // Formateamos la fecha para la URL: 2025-05-07
            var dateString = date.ToString("yyyy-MM-dd");

            // Llamamos a GET /api/ExchangeRate/{date}
            var dto = await _http
                .GetFromJsonAsync<EExchangeRateHistory>($"api/ExchangeRate/{dateString}");

            if (dto == null)
                throw new InvalidOperationException($"No se obtuvo la cotización para la fecha {dateString}.");
            return dto;
        }
    }
}
