using SecureShopContracts.BindingModels;
using SecureShopContracts.SearchModels;
using SecureShopContracts.StoragesContracts;
using SecureShopContracts.ViewModels;
using SecureShopDatabaseImplement.Models;

namespace SecureShopDatabaseImplement.Implements
{
    public class FacilitiesStorage : IFacilitiesStorage
    {
        public List<FacilitiesViewModel> GetFullList()
        {
            using var context = new SecureShopDatabase();
            return context.Facilitiess
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public List<FacilitiesViewModel> GetFilteredList(FacilitiesSearchModel model)
        {
            if (string.IsNullOrEmpty(model.FacilitiesName))
            {
                return new();
            }
            using var context = new SecureShopDatabase();
            return context.Facilitiess
                    .Where(x => x.FacilitiesName.Contains(model.FacilitiesName))
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public FacilitiesViewModel? GetElement(FacilitiesSearchModel model)
        {
            if (string.IsNullOrEmpty(model.FacilitiesName) && !model.Id.HasValue)
            {
                return null;
            }
            using var context = new SecureShopDatabase();
            return context.Facilitiess
                    .FirstOrDefault(x => (!string.IsNullOrEmpty(model.FacilitiesName) && x.FacilitiesName == model.FacilitiesName) ||
                                        (model.Id.HasValue && x.Id == model.Id))
                    ?.GetViewModel;
        }

        public FacilitiesViewModel? Insert(FacilitiesBindingModel model)
        {
            var newFacilities = Facilities.Create(model);
            if (newFacilities == null)
            {
                return null;
            }
            using var context = new SecureShopDatabase();
            context.Facilitiess.Add(newFacilities);
            context.SaveChanges();
            return newFacilities.GetViewModel;
        }

        public FacilitiesViewModel? Update(FacilitiesBindingModel model)
        {
            using var context = new SecureShopDatabase();
            var Facilities = context.Facilitiess.FirstOrDefault(x => x.Id == model.Id);
            if (Facilities == null)
            {
                return null;
            }
            Facilities.Update(model);
            context.SaveChanges();
            return Facilities.GetViewModel;
        }

        public FacilitiesViewModel? Delete(FacilitiesBindingModel model)
        {
            using var context = new SecureShopDatabase();
            var element = context.Facilitiess.FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Facilitiess.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
