using CandidateReviewContracts.BindingModels;
using CandidateReviewContracts.BusinessLogicsContracts;
using CandidateReviewContracts.SearchModels;
using CandidateReviewContracts.StoragesContracts;
using CandidateReviewContracts.ViewModels;
using CandidateReviewDatabaseImplement.Implements;
using CandidateReviewDatabaseImplement.Models;
using Microsoft.Extensions.Logging;
using System.Xml.Linq;

namespace CandidateReviewBusinessLogic.BusinessLogic
{
    public class AssessmentCriterionLogic : IAssessmentCriterionLogic
    {
        private readonly ILogger _logger;
        private readonly IAssessmentCriterionStorage _assessmentCriterionStorage;
        private readonly ICriterionStorage _criterionStorage;

        public AssessmentCriterionLogic(ILogger<AssessmentLogic> logger, IAssessmentCriterionStorage assessmentCriterionStorage, ICriterionStorage criterionStorage)
        {
            _logger = logger;
            _assessmentCriterionStorage = assessmentCriterionStorage;
            _criterionStorage = criterionStorage;
        }
        public bool Create(AssessmentCriterionModel model)
        {
            if (_assessmentCriterionStorage.Insert(model) == null)
            {
                _logger.LogWarning("Insert operation failed");
                return false;
            }
            return true;
        }

        public bool Delete(AssessmentCriterionModel model)
        {
            throw new NotImplementedException();
        }

        public AssessmentCriterionViewModel? ReadElement(AssessmentCriterionSearchModel model)
        {
            throw new NotImplementedException();
        }

        public List<AssessmentCriterionViewModel>? ReadList(AssessmentCriterionSearchModel? model)
        {
            var list = model == null ? _assessmentCriterionStorage.GetFullList() : _assessmentCriterionStorage.GetFilteredList(model);

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

            List<AssessmentCriterionViewModel> result = new();
            foreach (var item in  list)
            {
                var viewModel = new AssessmentCriterionViewModel
                {
                    Id = item.Id,
                    AssessmentId = item.AssessmentId,
                    CriterionId = item.CriterionId,
                    CriterionName = _criterionStorage.GetElement(new CriterionSearchModel { Id = item.CriterionId }).Name,
                    Value = item.Value
                };
                result.Add(viewModel);
            }
            
            _logger.LogInformation("ReadList. Count: {Count}", result.Count);
            return result;
        }

        public bool Update(AssessmentCriterionModel model)
        {
            throw new NotImplementedException();
        }
    }
}
