using Microsoft.EntityFrameworkCore;
using FishFactoryContracts.BindingModels;
using FishFactoryContracts.SearchModels;
using FishFactoryContracts.StoragesContracts;
using FishFactoryContracts.ViewModels;
using FishFactoryDatabaseImplement.Models;

namespace FishFactoryDatabaseImplement.Implements
{
    public class ImplementerStorage : IImplementerStorage
    {
        public ImplementerViewModel? Delete(ImplementerBindingModel model)
        {
            using var context = new FishFactoryDatabase();
            var element = context.Implementers.Include(x => x.Orders).FirstOrDefault(x => x.Id == model.Id);
            if (element != null)
            {
                context.Implementers.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

        public ImplementerViewModel? GetElement(ImplementerSearchModel model)
        {
            using var context = new FishFactoryDatabase();
            if (model.Id.HasValue)
            {
                return context.Implementers
                    .Include(x => x.Orders)
                    .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
            }
            else if (!string.IsNullOrEmpty(model.ImplementerFIO) && !string.IsNullOrEmpty(model.Password))
            {
                return context.Implementers.Include(x => x.Orders)
                    .FirstOrDefault(x => x.ImplementerFIO == model.ImplementerFIO && x.Password == model.Password)?.GetViewModel;
            }
            else if (!string.IsNullOrEmpty(model.ImplementerFIO))
            {
                return context.Implementers.Include(x => x.Orders)
                    .FirstOrDefault(x => x.ImplementerFIO == model.ImplementerFIO)?.GetViewModel;
            }
            return null;
        }

        public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ImplementerFIO))
            {
                return new();
            }
            using var context = new FishFactoryDatabase();
            return context.Implementers
                    .Include(x => x.Orders)
                    .Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO))
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public List<ImplementerViewModel> GetFullList()
        {
            using var context = new FishFactoryDatabase();
            return context.Implementers
                    .Include(x => x.Orders)
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public ImplementerViewModel? Insert(ImplementerBindingModel model)
        {
            var newImplementer = Implementer.Create(model);
            if (newImplementer == null)
            {
                return null;
            }
            using var context = new FishFactoryDatabase();
            context.Implementers.Add(newImplementer);
            context.SaveChanges();
            return context.Implementers
                .Include(x => x.Orders)
                .FirstOrDefault(x => x.Id == newImplementer.Id)?.GetViewModel;
        }

        public ImplementerViewModel? Update(ImplementerBindingModel model)
        {
            using var context = new FishFactoryDatabase();
            var implementer = context.Implementers.Include(x => x.Orders).FirstOrDefault(x => x.Id == model.Id);
            if (implementer == null)
            {
                return null;
            }
            implementer.Update(model);
            context.SaveChanges();
            return implementer.GetViewModel;
        }
    }
}
