
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StoragesContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;

namespace DatabaseImplement.Implements
{
	public class WorkshopStorage : IWorkshopStorage
	{
		public WorkshopViewModel? Delete(WorkshopBindingModel model)
		{
			using var context = new FactoryGoWorkDatabase();
			var newWorkshop = context.Workshops.FirstOrDefault(x => x.Id == model.Id);
			if (newWorkshop == null)
				return null;
			newWorkshop.UpdateWorkers(context, model);
			context.Workshops.Remove(newWorkshop);
			context.SaveChanges();
			return newWorkshop.GetViewModel;
		}

		public WorkshopViewModel? GetElement(WorkshopSearchModel model)
		{
			using var context = new FactoryGoWorkDatabase();
            return context.Workshops.Include(x => x.Workers).ThenInclude(x => x.Worker).Include(x => x.Production).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Title) && x.Title.Equals(model.Title)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
        }

		public List<WorkshopViewModel> GetFilteredList(WorkshopSearchModel model)
		{
            if (!model.UserId.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.DetailId.HasValue)
            {
                return new();
            }
            using var context = new FactoryGoWorkDatabase();
			if (model.DetailId.HasValue)
				return context.Workshops.Where(x => x.ProductionId.HasValue).Include(x => x.Production).Where(x => x.Production.Details.FirstOrDefault(y => y.DetailId == model.DetailId) != null).Where(x => x.UserId == model.UserId).Select(x => x.GetViewModel).ToList();
			else if (model.DateFrom.HasValue)
                return context.Workshops.Where(x => x.UserId == model.UserId).Where(x => x.DateCreate <= model.DateTo && x.DateCreate >= model.DateFrom).Select(x => x.GetViewModel).ToList();
            else
                return context.Workshops.Where(x => x.UserId == model.UserId).Include(x => x.Production).Select(x => x.GetViewModel).ToList();
        }

		public List<WorkshopViewModel> GetFullList()
		{
			using var context = new FactoryGoWorkDatabase();
            return context.Workshops.Include(x => x.Workers).ThenInclude(x => x.Worker).Select(x => x.GetViewModel).ToList();
        }

		public WorkshopViewModel? Insert(WorkshopBindingModel model)
		{
			using var context = new FactoryGoWorkDatabase();
			var newWorkshop = Workshop.Create(context, model);
			if (newWorkshop == null)
				return null;
			context.Workshops.Add(newWorkshop);
			context.SaveChanges();
			return newWorkshop.GetViewModel;
		}

		public WorkshopViewModel? Update(WorkshopBindingModel model)
		{
			using var context = new FactoryGoWorkDatabase();
			var newWorkshop = context.Workshops.FirstOrDefault(x => x.Id == model.Id);
			if (newWorkshop == null)
				return null;
			newWorkshop.Update(model);
			newWorkshop.UpdateWorkers(context, model);
			context.SaveChanges();
			return newWorkshop.GetViewModel;
		}
	}
}
