using AutoMapper;
using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1.Responses;
using PARR.Core.Repositories.Interfaces;
using PARR.Domain.Entities.Base.History;

namespace PARR.API.MappingProfiles.Resolvers
{
    public class HistoryInitiatorUserResolver : IValueResolver<IHistoryInitiator, HistoryInitiatorResponse, UserBaseResponse?>
    {
        private readonly IUserRepository userService;

        public HistoryInitiatorUserResolver(IUserRepository userService)
        {
            this.userService = userService;
        }

        public UserBaseResponse? Resolve(IHistoryInitiator source, HistoryInitiatorResponse destination, UserBaseResponse? destMember, ResolutionContext context)
        {
            var user = userService.Get().FirstOrDefaultAsync(t => t.Ip == source.InitiatorIp).GetAwaiter().GetResult();

            if (user == null)
                return null;

            return new UserBaseResponse { Ip = user.Ip, Description = user.Description ?? string.Empty, Name = user.Name };
        }
    }
}
