using CandidateReviewContracts.BindingModels;
using CandidateReviewContracts.SearchModels;
using CandidateReviewContracts.StoragesContracts;
using CandidateReviewContracts.ViewModels;
using CandidateReviewDatabaseImplement.Models;

namespace CandidateReviewDatabaseImplement.Implements
{
    public class AssessmentCriterionStorage : IAssessmentCriterionStorage
    {
        public AssessmentCriterionViewModel? Insert(AssessmentCriterionModel model)
        {
            var newAssessmentCriterion = AssessmentCriterion.Create(model);
            if (newAssessmentCriterion == null)
            {
                return null;
            }
            using var context = new CandidateReviewDatabase();
            context.AssessmentCriterions.Add(newAssessmentCriterion);
            context.SaveChanges();
            return newAssessmentCriterion.GetViewModel;
        }

        public List<AssessmentCriterionViewModel> GetFullList()
        {
            using var context = new CandidateReviewDatabase();
            return context.AssessmentCriterions
                .Select(x => x.GetViewModel)
                .ToList();
        }
        public List<AssessmentCriterionViewModel> GetFilteredList(AssessmentCriterionSearchModel model)
        {
            
            using var context = new CandidateReviewDatabase();
            if (model.AssessmentId.HasValue)
            {
                return context.AssessmentCriterions
               .Where(x => x.AssessmentId == model.AssessmentId)
               .Select(x => x.GetViewModel)
               .ToList();
            }
            if (!model.Id.HasValue)
            {
                return new();
            }
            return context.AssessmentCriterions
                .Where(x => x.Id == model.Id)
                .Select(x => x.GetViewModel)
                .ToList();
        }
    }
}
