using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StorageContracts;
using HospitalContracts.ViewModels;
using Microsoft.Extensions.Logging;

namespace HospitalBusinessLogic
{
    public class TreatmentLogic : ITreatmentLogic
    {
        private readonly ILogger _logger;
        private readonly ITreatmentStorage _treatmentStorage;

        public TreatmentLogic(ILogger<TreatmentLogic> logger, ITreatmentStorage treatmentStorage)
        {
            _logger = logger;
            _treatmentStorage = treatmentStorage;
        }

        public TreatmentViewModel? ReadElement(TreatmentSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            _logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
            var element = _treatmentStorage.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<TreatmentViewModel>? ReadList(TreatmentSearchModel? model)
        {
            _logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
            var list = model == null ? _treatmentStorage.GetFullList() : _treatmentStorage.GetFilteredList(model);
            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }
            _logger.LogInformation("ReadList. Count:{Count}", list.Count);
            return list;
        }
    }
}