using Microsoft.Extensions.Logging;
using SushiBarContracts.BindingModel;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.SearchModel;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;

namespace SushiBarBusinessLogic
{
    public class MessageInfoLogic : IMessageInfoLogic
    {

        private readonly ILogger logger;
        private readonly IMessageInfoStorage messageStorage;

        public MessageInfoLogic(ILogger<MessageInfoLogic> logger, IMessageInfoStorage messageStorage)
        {
            this.logger = logger;
            this.messageStorage = messageStorage;
        }
        public bool Create(MessageInfoBindingModel model)
        {
            if(messageStorage.Insert(model) == null)
            {
                logger.LogWarning("Insert message operation failed");
                return false;
            }
            return true;
        }

        public List<MessageInfoViewModel>? ReadList(MessageInfoSearchModel? model)
        {
            var list = model == null ? messageStorage.GetFullList() : messageStorage.GetFilteredList(model);
            if(list == null)
            {
                logger.LogWarning("ReadList had returned a null list");
                return null;
            }
            logger.LogInformation($"ReadList. Count of elements: {list.Count}");
            return list;
        }
    }
}
