using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.Converters;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BusinessLogic.BusinessLogic
{
    public class SellLogic : ISellLogic
    {
        private readonly ISellStorage _sellStorage;
        private readonly ILogger _logger;

        public SellLogic(ISellStorage sellStorage, ILogger<SellLogic> logger)
        {
            _sellStorage = sellStorage;
            _logger = logger;
        }
        public SellViewModel Create(SellBindingModel model)
        {
            ArgumentNullException.ThrowIfNull(model);
            var sell = _sellStorage.Insert(model);
            if (sell is null)
            {
                throw new Exception("Insert operation failed.");
            }
            return sell;
        }      
        public SellViewModel ReadElement(SellSearchModel model)
        {
            ArgumentNullException.ThrowIfNull(model);
            var sell = _sellStorage.GetElement(model);
            if (sell is null)
            {
                throw new Exception("Get element operation failed.");
            }
            return sell;
        }

        public IEnumerable<SellViewModel> ReadElements(SellSearchModel? model)
        {
            var sell_list = _sellStorage.GetFullList(model);
            if (sell_list is null || sell_list.Count() == 0)
            {
                _logger.LogWarning("ReadList return null list");
                return [];
            }
            return sell_list;
        }

        public SellViewModel Update(SellSearchModel model)
        {
            ArgumentNullException.ThrowIfNull(model);
            var sell = _sellStorage.GetElement(model);
            if (sell is null)
            {
                throw new Exception("Update operation failed.");
            }
            return sell;
        }
        public SellViewModel Delete(SellSearchModel model)
        {
            ArgumentNullException.ThrowIfNull(model);
            var sell = _sellStorage.Delete(model);
            if (sell is null)
            {
                throw new Exception("Update operation failed.");
            }
            return sell;
        }
    }
}
