using CarCenterContracts.ViewModels;
using CarCenterDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using CarCenterContracts.SearchModels;
using CarCenterContracts.BindingModels;
using CarCenterContracts.StoragesContracts;

namespace CarCenterDataBaseImplement.Implements
{
    public class InspectionStorage : IInspectionStorage
    {
        public List<InspectionViewModel> GetFullList()
        {
            using var context = new CarCenterDataBase();

            return context.Inspections
                .Include(x => x.Cars)
                .ThenInclude(x => x.Car)
                .ThenInclude(x => x.EquipmentCars) 
                .ThenInclude(x => x.Equipment)
                .Include(x => x.Employee)
                .Include(x => x.Administrator)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public InspectionViewModel? GetElement(InspectionSearchModel model)
        {
            if (string.IsNullOrEmpty(model.InspectionName) && !model.Id.HasValue)
            {
                return null;
            }

            using var context = new CarCenterDataBase();

            return context.Inspections
                .Include(x => x.Cars)
                .ThenInclude(x => x.Car)
                .ThenInclude(x => x.EquipmentCars)
                .ThenInclude(x => x.Equipment)
                .Include(x => x.Employee)
                .Include(x => x.Administrator)
                .FirstOrDefault(x => (!string.IsNullOrEmpty(model.InspectionName) && x.InspectionName == model.InspectionName) || (model.Id.HasValue && x.Id == model.Id))?
               .GetViewModel;
        }
        public List<InspectionViewModel> GetFilteredList(InspectionSearchModel model)
        {
            if (!model.AdministratorId.HasValue)
            {
                return new();
            }
            using var context = new CarCenterDataBase();
            if (!model.DateFrom.HasValue && !model.DateTo.HasValue && model.DateFrom.HasValue)
            {
                return context.Inspections
                       .Include(x => x.Cars)
                       .ThenInclude(x => x.Car)
                       .ThenInclude(x => x.EquipmentCars)
                       .ThenInclude(x => x.Equipment)
                       .Include(x => x.Employee)
                       .Include(x => x.Administrator)
                        .Where(x => x.InspectionDate >= model.DateFrom && x.InspectionDate <= model.DateTo && x.AdministratorId == model.AdministratorId)
                        .Select(x => x.GetViewModel)
                        .ToList();
            }
            else if(model.AdministratorId.HasValue)
            {
                return context.Inspections
                    .Include(x => x.Cars)
                    .ThenInclude(x => x.Car)
                    .ThenInclude(x => x.EquipmentCars)
                    .ThenInclude(x => x.Equipment)
                    .Include(x => x.Employee)
                    .Include(x => x.Administrator)
                    .Where(x => x.AdministratorId == model.AdministratorId)
                    .ToList()
                    .Select(x => x.GetViewModel)
                    .ToList();
            }
            return context.Inspections
                    .Include(x => x.Cars)
                    .ThenInclude(x => x.Car)
                    .ThenInclude(x => x.EquipmentCars)
                    .ThenInclude(x => x.Equipment)
                    .Include(x => x.Employee)
                    .Include(x => x.Administrator)
                .Where(x => x.InspectionName.Contains(model.InspectionName))
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public InspectionViewModel? Insert(InspectionBindingModel model)
        {
            using var context = new CarCenterDataBase();
            var newInspection = Inspection.Create(context, model);

            if (newInspection == null)
            {
                return null;
            }

            context.Inspections.Add(newInspection);
            context.SaveChanges();

            return context.Inspections
                .Include(x => x.Cars)
                .ThenInclude(x => x.Car)
                .ThenInclude(x => x.EquipmentCars)
                .ThenInclude(x => x.Equipment)
                .Include(x => x.Employee)
                .Include(x => x.Administrator)
                .FirstOrDefault(x => x.Id == newInspection.Id)
                ?.GetViewModel;
        }


        public InspectionViewModel? Update(InspectionBindingModel model)
        {
            using var context = new CarCenterDataBase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var elem = context.Inspections
                     .Include(x => x.Cars)
                     .ThenInclude(x => x.Car)
                     .ThenInclude(x => x.InspectionCar)
                     .ThenInclude(x => x.Inspection)
                     .ThenInclude(x => x.Employee)
                     .FirstOrDefault(rec => rec.Id == model.Id); if (elem == null)
                {
                    return null;
                }
				/*elem.Update(model);
                context.SaveChanges();
                if (model.InspectionCars != null)
                    elem.UpdateCars(context, model);
                transaction.Commit();
                return elem.GetViewModel;*/

				elem.UpdateCars(context, model);
				context.SaveChanges();
				elem.Update(model);
				context.SaveChanges();
				transaction.Commit();
				return elem.GetViewModel;
			}
            catch
            {
                transaction.Rollback();
                throw;
            }
        }

        public InspectionViewModel? Delete(InspectionBindingModel model)
        {
            using var context = new CarCenterDataBase();

            var element = context.Inspections
                .Include(x => x.Cars)
                .FirstOrDefault(rec => rec.Id == model.Id);

            if (element != null)
            {
                context.Inspections.Remove(element);
                context.SaveChanges();

                return element.GetViewModel;
            }

            return null;
        }


    }
}
