using VetClinicContracts.BindingModels;
using VetClinicContracts.BusinessLogicsContracts;
using VetClinicContracts.SearchModels;
using VetClinicContracts.StoragesContracts;
using VetClinicContracts.ViewModels;
using Microsoft.Extensions.Logging;

namespace VetClinicBusinessLogic.BusinessLogics
{
    public class AdminLogic : IAdminLogic
    {
        private readonly ILogger _logger;
        private readonly IAdminStorage _adminStorage;
        public AdminLogic(ILogger<AdminLogic> logger, IAdminStorage adminStorage)
        {
            _logger = logger;
            _adminStorage = adminStorage;
        }
        public bool Create(AdminBindingModel model)
        {
            CheckModel(model);
            if (_adminStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }
            return true;
        }

        public bool Delete(AdminBindingModel model)
        {
            CheckModel(model, false);
            _logger.LogInformation("Delete. Id:{Id}", model.Id);
            if (_adminStorage.Delete(model) == null)
            {
                _logger.LogWarning("Delete operation failed");
                return false;
            }
            return true;
        }

        public AdminViewModel? ReadElement(AdminSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadList. AdminFIO:{AdminFIO}. Id:{Id}", model.AdminFIO, model?.Id);

            var element = _adminStorage.GetElement(model);
            if (element == null)
            {
                _logger.LogWarning("ReadElement element not found");
                return null;
            }
            _logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
            return element;
        }

        public List<AdminViewModel>? ReadList(AdminSearchModel? model)
        {
            _logger.LogInformation("ReadList. ClientFIO:{ClientFIO}. Id:{Id}", model?.AdminFIO, model?.Id);
            var list = model == null ? _adminStorage.GetFullList() : _adminStorage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }
            _logger.LogInformation("ReadList. Count:{Count}", list.Count);
            return list;
        }

        public bool Update(AdminBindingModel model)
        {
            CheckModel(model);
            if (_adminStorage.Update(model) == null)
            {
                _logger.LogWarning("Update operation failed");
                return false;
            }
            return true;
        }
        private void CheckModel(AdminBindingModel model, bool withParams = true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (!withParams)
            {
                return;
            }
            if (string.IsNullOrEmpty(model.AdminFIO))
            {
                throw new ArgumentNullException("Отсутствие ФИО в учётной записи", nameof(model.AdminFIO));
            }
            if (string.IsNullOrEmpty(model.Email))
            {
                throw new ArgumentNullException("Отсутствие почты в учётной записи (логина)", nameof(model.Email));
            }
            if (string.IsNullOrEmpty(model.Password))
            {
                throw new ArgumentNullException("Отсутствие пароля в учётной записи", nameof(model.Password));
            }

            _logger.LogInformation("WorkPiece. AdminFIO:{AdminFIO}. Email:{Email}. Password:{Password}. Id:{Id}",
                model.AdminFIO, model.Email, model.Password, model.Id);
            var element = _adminStorage.GetElement(new AdminSearchModel
            {
                Email = model.Email,
            });
            if (element != null && element.Id != model.Id)
            {
                throw new InvalidOperationException("Аккаунт с таким логином уже есть");
            }
        }
    }
}
