using Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Models.Helpers;
using Models.Interfaces;
using Models.Models;

namespace Models.Repositories
{
    public class PhOHExchangeRateHistory(PhronCareOperationsHubContext context) : IPhOHExchangeRateHistory
    {
        #region Declaraciones y Constructor
        private readonly PhronCareOperationsHubContext _context = context;
        #endregion

        #region Metodos de clase
        public async Task<EExchangeRateHistory?> GetByDateAsync(DateOnly date)
        {
            var ph = await _context.PhOhExchangeRateHistories
                .AsNoTracking()
                .FirstOrDefaultAsync(x => x.Ratedate == date);

            return ph is null
                ? null
                : EntityMapper.MapEntity<PhOhExchangeRateHistory, EExchangeRateHistory>(ph);
        }
        public async Task<EExchangeRateHistory> AddAsync(EExchangeRateHistory entity)
        {
            // 1) Mapea tu modelo de dominio a la entidad EF
            var phEntity = EntityMapper.MapEntity<EExchangeRateHistory, PhOhExchangeRateHistory>(entity);

            // 2) Agrégalo al contexto y guarda
            await _context.PhOhExchangeRateHistories.AddAsync(phEntity);
            await _context.SaveChangesAsync();

            // 3) Mapea de vuelta y retorna el modelo de dominio con el Id generado
            return EntityMapper.MapEntity<PhOhExchangeRateHistory, EExchangeRateHistory>(phEntity);
        }

        #endregion
    }
}

