
using Core.Interfaces;
using Domain.Entities;
using Microsoft.AspNetCore.Mvc;


namespace phronCare.API.Controllers.Integrations
{
    [ApiController]
    [Route("api/[controller]")]
    public class ExchangeRateController : ControllerBase
    {
        private readonly IExchangeRateDom _exchangeRateDom;

        public ExchangeRateController(IExchangeRateDom exchangeRateDom)
        {
            _exchangeRateDom = exchangeRateDom;
        }

        /// <summary>
        /// Devuelve la cotización oficial del dólar del día anterior,
        /// leyendo de histórico o consultando BCRA si hace falta.
        /// </summary>
        [HttpGet("yesterday")]
        public async Task<ActionResult<EExchangeRateHistory>> GetYesterdayAsync()
        {
            try
            {
                var rate = await _exchangeRateDom.GetYesterdayRateAsync();
                return Ok(rate);
            }
            catch (Exception ex)
            {
                // Aquí puedes loggear el error o transformarlo en un código HTTP distinto
                return StatusCode(500, new { message = ex.Message });
            }
        }

        /// <summary>
        /// Obtiene la cotización del dólar oficial para una fecha dada
        /// </summary>
        [HttpGet("{date:datetime}")]
        public async Task<ActionResult<EExchangeRateHistory>> GetByDateAsync(DateTime date)
        {
            var dateOnly = DateOnly.FromDateTime(date);
            var existing = await _exchangeRateDom.GetByDateAsync(dateOnly);
            if (existing is null)
                return NotFound(new { message = $"No hay cotización para {dateOnly:yyyy-MM-dd}" });
            return Ok(existing);
        }

    }
}
