using CandidateReviewContracts.BindingModels;
using CandidateReviewContracts.BusinessLogicsContracts;
using CandidateReviewContracts.SearchModels;
using CandidateReviewContracts.StoragesContracts;
using CandidateReviewContracts.ViewModels;
using CandidateReviewDataModels.Models;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;

namespace CandidateReviewBusinessLogic.BusinessLogic
{
    public class AssessmentLogic : IAssessmentLogic
    {
        private readonly ILogger _logger;
        private readonly IAssessmentStorage _assessmentStorage;
        private readonly IUserStorage _userStorage;
        private readonly ICriterionStorage _criterionStorage;
        private readonly IAssessmentCriterionStorage _assessmentCriterionStorage;

        public AssessmentLogic(ILogger<AssessmentLogic> logger, IAssessmentStorage assessmentStorage, IUserStorage userStorage, ICriterionStorage criterionStorage, IAssessmentCriterionStorage assessmentCriterionStorage)
        {
            _logger = logger;
            _assessmentStorage = assessmentStorage;
            _userStorage = userStorage;
            _criterionStorage = criterionStorage;
            _assessmentCriterionStorage = assessmentCriterionStorage;
        }

        public int? Create(AssessmentBindingModel model)
        {
            CheckModel(model);
            var id = _assessmentStorage.Insert(model);
            if (id == null)
            {
                _logger.LogWarning("Insert operation failed");
                return 0;
            }
            return id;
        }

        

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

        public AssessmentViewModel? ReadElement(AssessmentSearchModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            var element = _assessmentStorage.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<AssessmentViewModel>? ReadList(AssessmentSearchModel? model)
        {
            var list = model == null ? _assessmentStorage.GetFullList() : _assessmentStorage.GetFilteredList(model);

            if (list == null)
            {
                _logger.LogWarning("ReadList return null list");
                return null;
            }

            List<AssessmentViewModel> result = new();
            foreach (var item in list)
            {
                var viewModel = new AssessmentViewModel
                {
                    Id = item.Id,
                    ResumeId = item.ResumeId,
                    UserId = item.UserId,
                    UserName = _userStorage.GetElement(new UserSearchModel { Id = item.UserId }).Name + " " + _userStorage.GetElement(new UserSearchModel { Id = item.UserId }).Surname,
                    Comment = item.Comment
                };
                result.Add(viewModel);
            }

            _logger.LogInformation("ReadList. Count: {Count}", result.Count);
            return result;
        }

        public bool Update(AssessmentBindingModel model)
        {
            CheckModel(model);
            if (_assessmentStorage.Update(model) == null)
            {
                _logger.LogWarning("Update operation failed");
                return false;
            }
            return true;
        }

        private void CheckModel(AssessmentBindingModel model, bool withParams = true)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (!withParams)
            {
                return;
            }

            var element = _assessmentStorage.GetElement(new AssessmentSearchModel
            {
                ResumeId = model.ResumeId,
                UserId = model.UserId
            });

            if (element != null && element.Id != model.Id)
            {
                throw new InvalidOperationException("Резюме уже было оценено");
            }
        }
    }
}
