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 HistoryInitiatorParrComponentResolver : IValueResolver<IHistoryInitiator, HistoryInitiatorResponse, ParrComponentResponse?>
    {
        private readonly IParrComponentRepository parrComponentService;

        public HistoryInitiatorParrComponentResolver(IParrComponentRepository parrComponentService)
        {
            this.parrComponentService = parrComponentService;
        }

        public ParrComponentResponse? Resolve(IHistoryInitiator source, HistoryInitiatorResponse destination, ParrComponentResponse? destMember, ResolutionContext context)
        {
            var component = parrComponentService.Get().FirstOrDefaultAsync(t => t.Id == source.InitiatorParrComponentId).GetAwaiter().GetResult();

            if (component == null)
                return null;

            return new ParrComponentResponse { Description = component.Description, Name = component.Name, Id = (int)component.Id };
        }
    }
}
